开发者

Using C#, how to get one cell text from edited row?

开发者 https://www.devze.com 2023-01-29 01:43 出处:网络
Hy, I have a gridView control in Asp.NET like this: <asp:GridView ID=\"outputGridView\" runat=\"server\" onrowediting=\"OutputGridView_RowEditing\">

Hy,

I have a gridView control in Asp.NET like this:

<asp:GridView ID="outputGridView" runat="server" onrowediting="OutputGridView_RowEditing">
 <asp:TemplateField ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Middle"
                        ItemStyle-Width="250px" HeaderText="JobId" HeaderStyle-HorizontalAlign="Left"
                        HeaderStyle-BorderWidth="1px" HeaderStyle-BorderColor="#e1e1e1">
                        <ItemTemplate>
                            <%# Eval("JobId")%>
                        </ItemTemplate>
                        <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Font-Bold="True"></HeaderStyle>
                        <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="250px" BorderWidth="1px"
                            BorderColor="#e1e1e1"></ItemStyle>
                    </asp:TemplateField>

</aspGridView>开发者_运维技巧

On OutputGridView_RowEditing I have this code:

protected void OutputGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridViewRow currentRow = outputGridView.Rows[e.NewEditIndex];
            string JobId = currentRow.Cells[2].Text;
            e.Cancel = true;
        }

But in 'JobId' string its "", does anyone have any idea how can I get the text of the third cell from the row that is being edited?

Thank you,

Jeff


Ok what Bonshington said is correct, accept you want to add an id to the label.

<ItemTemplate>
    <asp:Label ID="LblJobId" runat="server" Text='<%# Eval("JobId") %>' />
</ItemTemplate>

protected void OutputGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridViewRow currentRow = outputGridView.Rows[e.NewEditIndex];
    Label jobIdLabel = (Label)currentRow.Cells[2].FindControl("LblJobId");
    string jobId = jobIdLabel.Text;
    e.Cancel = true;
}


Please use Bind() method instead of Eval() method, it is for evaluation purpose only.


try to put it in literal cotnrol

<label><%# Eval("JobId")%></label>

and your jobID column will positioned as cell's child control


if you want to get GridViewRow currentRow you have to use

<Columns>
    <asp:TemplateField>
    <EditItemTemplate><asp:Label id="lbl" Text="<%# Eval("JobId")%>" /></EditItemTemplate>
    </asp:TemplateField>
</Columns>

Auto-generated grid columns use Cells property

 protected void OutputGridView_RowEditing(object sender, GridViewEditEventArgs e)
 {
     OutputGridView.Rows[e.NewEditIndex].Cells[0]
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号