如何使用gridview中的hiddenfield在按钮点击&上找到id按钮在gridview C#之外 [英] How to find id using hiddenfield in gridview on button click & button is outside gridview C#
问题描述
如何在按钮点击&中使用Gridview中的Hiddenfield查找ID按钮在Gridview C#之外我正在尝试,但它显示错误对象引用未设置为对象的实例。在string strhfId =(gvRow.FindControl(hfId)作为HiddenField).Value;
我尝试了不同的
HiddenField hfId =(HiddenField)gridData.SelectedRow.Cells [0] .FindControl(hfId);
但错误相同
我尝试过:
protected void lnkUpdate_Click(object sender,EventArgs e)
{
// HiddenField hfId =(HiddenField)gridData.SelectedRow。细胞[0] .FindControl( HFID);
// HiddenField strId =((HiddenField)gridData.Rows [0] .FindControl(hfIdValue));
// String value = strId.Value;
LinkButton btn =发送者为LinkButton;
GridViewRow gvRow = btn.NamingContainer as GridViewRow;
string strhfId =(gvRow.FindControl(hfId)as HiddenField).Value;
try
{
// HiddenField hfIdc =(HiddenField)gridData.SelectedRow.Cells [0] .FindControl(hfIdValue);
列表< SqlParameter> parameters = new List< SqlParameter>
{
};
parameters.Add(new SqlParameter(@ P_APPLICATION_ID,strhfId));
parameters.Add(new SqlParameter(@ P_APPLICATION_NAME,txtApplicationName.Text));
parameters.Add(new SqlParameter(@ P_MODE,E));
string strResponse = objAppFunctionModule.ExecuteNonQueryMethod(strUSP_APPLICATION_MST,parameters);
PopulateGrid();
}
catch(例外情况)
{
lblResponse.Text = ex.Message;
}
}
Markup;
< asp:GridView ID =gridDatarunat =serverAutoGenerateColumns =FalseOnRowDataBound =gridData_RowDataBound
RowStyle- HorizontalAlign =CenterShowHeaderWhenEmpty =trueOnRowCommand =gridData_RowCommand
EmptyDataText =没有所选值的数据BackColor =WhiteBorderColor =#c5c5c5BorderWidth =1pxWidth =100%
CellPadding =4AllowPaging =True>
< columns>
< asp:templatefield>
< ItemStyle />
< HeaderStyle CssClass =ItemStyle/>
< headertemplate>
< asp:CheckBox ID =chkAllclass =cbAllrunat =serveronclick =javascript:SelectAllCheckboxes1(this); />
< itemtemplate>
< asp:CheckBox ID =chkMultiDeleterunat =server/>
< asp:TemplateField HeaderText =应用程序名称>
< ItemStyle />
< HeaderStyle CssClass =ItemStyle/>
< itemtemplate>
< asp:Label ID =lblApplicationNamerunat =serverText ='<%#Eval(APPLICATION_NAME)%>'>
< asp:HiddenField runat =serverID =hfIdValue ='<%#Eval(APPLICATION_ID)%>'>
< asp:TemplateField HeaderText =Edit>
< ItemStyle />
< HeaderStyle CssClass =ItemStyle/>
< itemtemplate>
< asp:LinkButton ID =lnkEditclass =btn btn-defaultCommandName =EditButtonCommandArgument ='<%#((GridViewRow)Container).RowIndex +; %>'runat =server> 编辑
当用户从gridview中选择一行时,想法是填充你的hiddenField。
首先你应该添加SelectedIndexchanged事件在Gridview标记中:
< asp:GridView ID =GridView1runat =server
AutoGenerateColumns =falseOnSelectedIndexChanged =OnSelectedIndexChanged>
后面的代码应如下所示:
protected void OnSelectedIndexChanged( object sender,EventArgs e)
{
// 使用列索引访问id字段:我认为你的索引是
// 第一列0
string id = GridView1.SelectedRow.Cells [ 0 ]。文字;
// 或
/ / 您可以按名称访问该字段:确保指定列
/ / 类型(本例中的标签)
string id =(GridView1。 SelectedRow.FindControl( id) as 标签)。文字;
// 将值绑定到隐藏字段
hfId.Text = id ; // 隐藏字段与Gridview无关,它只是一个html
// 标记,其中hfId为其ID
}
一旦用户点击输入按钮,你只需得到隐藏的字段值
protected void lnkUpdate_Click( object sender,EventArgs e)
{
int selectedId = Convert.toInt32(hfId.Text); // 这有点危险,因为// hfText值可以是非Int。更好地使用TryParse。
}
如果我误解了,并且隐藏字段在gridview中,之前显示的所有内容仍然有效,只需要填充一个全局变量而不是隐藏字段(当用户选择一行时),并在用户点击链接按钮时访问它。
希望它有所帮助。
更改命令参数以删除;
CommandArgument ='<%#( (GridViewRow)容器).RowIndex%>'
然后挂钩到网格的命令事件而不是链接按钮的点击事件
protected void gridData_RowCommand(object sender,GridViewCommandEventArgs e)
{
GridView g =(GridView)sender;
int index = int.Parse(e.CommandArgument.ToString());
GridViewRow row = g.Rows [index];
HiddenField hfId =(HiddenField)row.Cells [1] .FindControl(hfId);
}
你可能更好地使用gridview的内置可编辑行框架。
再次无法使用相同的错误
对象引用未设置为对象的实例。
How to find id using Hiddenfield in Gridview on button click & button is outside Gridview C# I am trying but its showing error "Object reference not set to an instance of an object." on "string strhfId = (gvRow.FindControl("hfId") as HiddenField).Value;" I have tried different too HiddenField hfId = (HiddenField)gridData.SelectedRow.Cells[0].FindControl("hfId"); but error is same
What I have tried:
protected void lnkUpdate_Click(object sender, EventArgs e) { //HiddenField hfId = (HiddenField)gridData.SelectedRow.Cells[0].FindControl("hfId"); //HiddenField strId = ((HiddenField)gridData.Rows[0].FindControl("hfIdValue")); //String value = strId.Value; LinkButton btn = sender as LinkButton; GridViewRow gvRow = btn.NamingContainer as GridViewRow; string strhfId = (gvRow.FindControl("hfId") as HiddenField).Value; try { //HiddenField hfIdc = (HiddenField)gridData.SelectedRow.Cells[0].FindControl("hfIdValue"); List<SqlParameter> parameters = new List<SqlParameter> { }; parameters.Add(new SqlParameter("@P_APPLICATION_ID", strhfId)); parameters.Add(new SqlParameter("@P_APPLICATION_NAME", txtApplicationName.Text)); parameters.Add(new SqlParameter("@P_MODE", "E")); string strResponse = objAppFunctionModule.ExecuteNonQueryMethod(strUSP_APPLICATION_MST, parameters); PopulateGrid(); } catch (Exception ex) { lblResponse.Text = ex.Message; } }
Markup;
<asp:GridView ID="gridData" runat="server" AutoGenerateColumns="False" OnRowDataBound="gridData_RowDataBound" RowStyle-HorizontalAlign="Center" ShowHeaderWhenEmpty="true" OnRowCommand="gridData_RowCommand" EmptyDataText="No Data for the Selected Value" BackColor="White" BorderColor="#c5c5c5" BorderWidth="1px" Width="100%" CellPadding="4" AllowPaging="True"> <columns> <asp:templatefield> <ItemStyle /> <HeaderStyle CssClass="ItemStyle" /> <headertemplate> <asp:CheckBox ID="chkAll" class="cbAll" runat="server" onclick="javascript:SelectAllCheckboxes1(this);" /> <itemtemplate> <asp:CheckBox ID="chkMultiDelete" runat="server" /> <asp:TemplateField HeaderText="Application Name"> <ItemStyle /> <HeaderStyle CssClass="ItemStyle" /> <itemtemplate> <asp:Label ID="lblApplicationName" runat="server" Text='<%#Eval("APPLICATION_NAME") %>'> <asp:HiddenField runat="server" ID="hfId" Value='<%#Eval("APPLICATION_ID")%>'> <asp:TemplateField HeaderText="Edit"> <ItemStyle /> <HeaderStyle CssClass="ItemStyle" /> <itemtemplate> <asp:LinkButton ID="lnkEdit" class="btn btn-default" CommandName="EditButton" CommandArgument='<%# ((GridViewRow) Container).RowIndex + ";" %>' runat="server">Edit
Hi,
The idea is to fill your hiddenField when user select a row from the gridview.
First you should add the SelectedIndexchanged event in the Gridview Tag :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnSelectedIndexChanged = "OnSelectedIndexChanged">
you code behind should look like this :
protected void OnSelectedIndexChanged(object sender, EventArgs e) { //Accessing the id field using Column index : i supposed your index is the //first column here "0" string id = GridView1.SelectedRow.Cells[0].Text; //Or //you can aslo access the field by its name : make sure to specify the column //type (label in this example) string id = (GridView1.SelectedRow.FindControl("id") as Label).Text; //bind the value to the hidden field hfId.Text = id; //the hidden filed is not related to Gridview, its just a html //tag with hfId as its Id }
once the user click on the input button, you just have to get the hidden field value
protected void lnkUpdate_Click(object sender, EventArgs e) { int selectedId = Convert.toInt32(hfId.Text); //this is a little risky since the //hfText value can be a non Int. better use a TryParse. }
If i misunderstood, and the Hidden field is in the gridview, everything showen earlier is still valid, you just have to fill a global variable instead of the hidden field (when user select a row), and access it when the user click the linkbutton.
Hope it helps.
Change the commandargument to drop the ";"
CommandArgument='<%# ((GridViewRow) Container).RowIndex %>'
Then hook into the grid's command event rather than the link button's click event
protected void gridData_RowCommand(object sender, GridViewCommandEventArgs e) { GridView g = (GridView)sender; int index = int.Parse(e.CommandArgument.ToString()); GridViewRow row = g.Rows[index]; HiddenField hfId = (HiddenField) row.Cells[1].FindControl("hfId"); }
You're probably better using the gridview's inbuilt editable row framework though.
hi,
not working again the same error
"Object reference not set to an instance of an object."
这篇关于如何使用gridview中的hiddenfield在按钮点击&上找到id按钮在gridview C#之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!