使用C#,如何从编辑的行中获取一个单元格文本?

时间:2010-12-07 12:28:53

标签: c# asp.net gridview

HY,

我在Asp.NET中有一个gridView控件,如下所示:

<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>

在OutputGridView_RowEditing上我有这段代码:

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

但是在'JobId'字符串中它“”,有没有人知道如何从正在编辑的行中获取第三个单元格的文本?

谢谢,

杰夫

4 个答案:

答案 0 :(得分:0)

请使用Bind()方法代替Eval()方法,它仅用于评估目的。

答案 1 :(得分:0)

尝试将其置于文字控制中

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

并且您的作业ID列将定位为单元格的子控件

答案 2 :(得分:0)

如果你想获得GridViewRow currentRow,你必须使用

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

自动生成的网格列使用Cells属性

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

答案 3 :(得分:0)

好的Bonshington说的是正确的,接受你想在标签上添加一个id。

<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;
}