C# Gridview所选行返回空值
在我的aspx文件中,gridview包含如下链接按钮:C# Gridview所选行返回空值,c#,asp.net,gridview,C#,Asp.net,Gridview,在我的aspx文件中,gridview包含如下链接按钮: <asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="Button3" Style="float: left; margin-left: 10px" CommandArgument='<%# Bind("ID") %>' Text="cancellation" runat
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Button3" Style="float: left; margin-left: 10px"
CommandArgument='<%# Bind("ID") %>'
Text="cancellation" runat="server" CommandName="cancell" OnClick="Button3_Click">
<i aria-hidden="true" class="icon-lock" title="cancellation"></i>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
dgvData.SelectedRow
不会提供单击按钮的行。为此,您需要NamingContainer
。这应该适合您:-
LinkButton Button3 = (LinkButton)sender;
GridViewRow selectedRow = (GridViewRow)Button3.NamingContainer;
string barcode = selectedRow.Cells[12].Text;
dgvData.SelectedRow
不会提供单击按钮的行。为此,您需要NamingContainer
。这应该适合您:-
LinkButton Button3 = (LinkButton)sender;
GridViewRow selectedRow = (GridViewRow)Button3.NamingContainer;
string barcode = selectedRow.Cells[12].Text;
这是因为您没有使用select命令 要走的路应该是:
- 将RowCommand事件添加到GridView
onrowcommand="GridView1_RowCommand"
- 用条形码替换命令参数
,并删除id
OnClick=“Button3\u单击”
- 将RowCommand事件添加到GridView
onrowcommand="GridView1_RowCommand"
- 用条形码替换命令参数
,并删除id
OnClick=“Button3\u单击”
- 这是因为您没有使用select命令
要走的路应该是:
protected void Button3_Click(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.Rows[e.RowIndex];
string barcode = row.Cells["ID"].Value.ToString();
}
}
但我可以从您的代码中看到,您正在使用GridView的编辑模式,但没有将其应用到您的代码中。请参见此以编辑和更新Asp.Net中GridView中的行。您可以尝试通过这种方式从GridView中获取值。我不是在写你的全部代码,而是在写包含错误的部分
protected void Button3_Click(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.Rows[e.RowIndex];
string barcode = row.Cells["ID"].Value.ToString();
}
}
但我可以从您的代码中看到,您正在使用GridView的编辑模式,但没有将其应用到您的代码中。请参阅此页以编辑和更新Asp.Net中GridView中的行。谢谢您的关注,我找到了它。 以下是我编辑的asp标记:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Button3" Style="float: left; margin-left: 10px"
CommandArgument='<%# Bind("ID") %>'
Text="cancellation" runat="server" **CommandName="select"** OnClick="Button3_Click">
<i aria-hidden="true" class="icon-lock" title="cancellation"></i>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
谢谢你的关心,我找到了。 以下是我编辑的asp标记:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Button3" Style="float: left; margin-left: 10px"
CommandArgument='<%# Bind("ID") %>'
Text="cancellation" runat="server" **CommandName="select"** OnClick="Button3_Click">
<i aria-hidden="true" class="icon-lock" title="cancellation"></i>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
属性将仅在选择行时填充该行。最简单的方法是将属性设置为true
,如下所示:
<asp:gridview id="CustomersGridView" autogenerateselectbutton="True" .../>
必须按顺序执行以下步骤:
dgvData.SelectedRow.Cells[12]。Text
表示单元格/列12应为边界字段
如果第12列是模板字段,则使用FindControl()
方法作为dgvData.SelectedRow.FindControl(“lblBarCode”)作为标签).Text
假设标签用于显示条形码等。属性仅在选择行时才会填充该行。最简单的方法是将属性设置为true
,如下所示:
<asp:gridview id="CustomersGridView" autogenerateselectbutton="True" .../>
必须按顺序执行以下步骤:
通过单击“选择”按钮选择一行
然后单击链接按钮。在按钮单击事件中,您将像往常一样拥有所选的行
另外,dgvData.SelectedRow.Cells[12]。Text
表示单元格/列12应为边界字段
如果第12列是模板字段,则使用FindControl()
方法作为dgvData.SelectedRow.FindControl(“lblBarCode”)作为标签).Text
假设标签用于显示条形码等。那么哪一行引发异常,以及您做了什么来调查原因?在这一行中:string barcode=dgvData.SelectedRow.Cells[12]。Text;否不是这种情况,my gridview的selected row属性返回null。@saeedgarmsiri-好的,如果您知道的话,您应该提到:)那么哪一行引发了异常,您做了什么来调查原因?在这一行中:string barcode=dgvData.SelectedRow.Cells[12]。Text;否不是这种情况,my gridview的selected row属性返回null。@saeedgarmsiri-好的,如果您知道,您应该提到:)