我用Gridview 自定义编辑 ,每行的主键的值能获取到, 但其他字段的值就获取不到, 返回的总是为空
前台代码:
<asp:TemplateField HeaderText="编辑">
<ItemTemplate>
<asp:LinkButton ID="LnkbtnEdit" CommandArgument='<%# Eval("Dpt_Name")%>' CausesValidation="false" runat="server" CommandName="Edit_New">更新</asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="40" HorizontalAlign="Center" />
</asp:TemplateField> 后台代码:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //索引
//String id = Convert.ToString(GridView1.DataKeys[drv.RowIndex].Value); //drv.RowIndex 下标
//上面这个获取主键id 能正确返回值
//但下面这个获取某行的某个单元格值, 获取不到, 返回的总是为空
GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent));
String id = Convert.ToString(GridView1.Rows[drv.RowIndex].Cells[2].Text);
if (e.CommandName == "Edit_New")
{
DbTools.MsgBox(id);
//ModalDlg.Show();
}
}
前台代码:
<asp:TemplateField HeaderText="编辑">
<ItemTemplate>
<asp:LinkButton ID="LnkbtnEdit" CommandArgument='<%# Eval("Dpt_Name")%>' CausesValidation="false" runat="server" CommandName="Edit_New">更新</asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="40" HorizontalAlign="Center" />
</asp:TemplateField> 后台代码:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //索引
//String id = Convert.ToString(GridView1.DataKeys[drv.RowIndex].Value); //drv.RowIndex 下标
//上面这个获取主键id 能正确返回值
//但下面这个获取某行的某个单元格值, 获取不到, 返回的总是为空
GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent));
String id = Convert.ToString(GridView1.Rows[drv.RowIndex].Cells[2].Text);
if (e.CommandName == "Edit_New")
{
DbTools.MsgBox(id);
//ModalDlg.Show();
}
}
<HeaderStyle Width="30%"></HeaderStyle> <EditItemTemplate>
<asp:LinkButton ID="LinkButton5" runat="server" CausesValidation="True" CommandName="update">保存</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="False" CommandName="cancel">取消</asp:LinkButton>
</EditItemTemplate>
<ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit" CausesValidation="false" OnClientClick="return confirm('确认要编辑吗?');">编辑</asp:LinkButton>
。cs protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string sId = gv.DataKeys[e.RowIndex].Value.ToString();
string strName = ((TextBox)gv.Rows[e.RowIndex].FindControl("Txt_Name")).Text.Trim();
....
}