我有一个GridView的按钮“删除”。 我需要获得值或索引的单元格[3]。 但与GridView1.SelectedRow.Cells[3].Text;
它返回null。 有人帮我吗?我不想使用JavaScript。获取列值GridView
我的aspx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" GridLines="None"
CssClass="table table-bordered table-striped">
<Columns>
<asp:BoundField DataField="DisplayName" HeaderText="Display Name" />
<asp:BoundField DataField="UserName" HeaderText="User Name" />
<asp:BoundField DataField="Email" HeaderText="Email" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_OnClick" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我的.cs:
protected void btnDelete_OnClick(object sender, EventArgs e)
{
using (objConexao = new SqlConnection(strStringConexao))
{
SqlConnection oConn = new SqlConnection();
SqlCommand oCommand = new SqlCommand();
string strConn = ConfigurationManager.ConnectionStrings["XXX"].ConnectionString;
oConn.ConnectionString = strConn;
oCommand.Connection = oConn;
oCommand.CommandText = "proc_Delete";
oCommand.CommandType = CommandType.StoredProcedure;
SqlParameter myParam = oCommand.Parameters.Add("@UserRequestor", SqlDbType.NVarChar);
myParam.Value = User;
SqlParameter myParam2 = oCommand.Parameters.Add("@UserName", SqlDbType.NVarChar);
myParam2.Value = GridView1.Columns[3].ToString();
oConn.Open();
oCommand.ExecuteNonQuery();
oConn.Close();
}
}
是你从你的'btnDelete_OnClick'方法提供的.cs代码? – wlyles
是的!我添加了这些信息。 – CaioVJesus89
尝试使用'e.Item.FindControl(“controlname”);'查找控件/单元格 – PiLHA