问题描述
我有一些列的GridView。所有的列都是绑定的:
< asp:BoundField DataField =orderIdHeaderText =orderId
的SortExpression = 订单ID >< / ASP:绑定列>
最后一栏是:
< asp:LinkButton ID =LinkButton1runat =server
CommandName =onclick =LinkButton1_ClickText =Button>< / asp:LinkButton>
正如你所看到的,有一些方法是onclick..就像:
lbltest.Text = gv_order.Rows [gv_order.SelectedIndex] .Cells [2] .Text;
通过这段代码,我得到了(offcourse)我在单元格编号为2的选定行上所拥有的内容。我可以从同一行(并从单元格编号2)中获取值,而单击该按钮时没有自选行?例如:当我点击第2行上的按钮 - 我得到该行的单元格2。
这可能吗?
< asp:GridView(...)OnRowCommand =Gv_RowCommand(...)>
< asp:LinkButton ID =LinkButton1runat =serverCommandName =Select
CommandArgument ='<%#绑定(orderId)%>'文本=按钮>< / asp:LinkButton>
以及后面的代码:
<$ p $
{
if(e.CommandName ==Select)
{
int selectedOrderId = Convert.ToInt32(e.CommandArgument);
// ...
}
}
我希望这是你想要做的。
编辑 - 我对您评论的回答:
然后,它稍微复杂一些,并以某种方式使用'selectedRow'。在我自己的代码中,我使用了这种方法:
< asp:GridView ID =gv1(...)DataKeyNames =orderId,email,username
OnRowCommand =Gv_RowCommand(...)>
< asp:LinkButton ID =LinkButton1runat =serverCommandName =Select
CommandArgument ='<%# DataBinder.Eval(Container,RowIndex)%>'Text =Button>
< / asp:LinkButton>
以及后面的代码:
protected void Gv_RowCommand(object sender,GridViewCommandEventArgs e)
{
int selectedRowIndex = Convert.ToInt32(e.CommandArgument);
if(e.CommandName ==Select)
{
int orderId = Convert.ToInt32(gv1.DataKeys [selectedRowIndex] [orderId]);
string email = gv1.DataKeys [selectedRowIndex] [email]。ToString();
// ...
}
}
I have GridView with some of columns. all the columns are boundfield like:
<asp:BoundField DataField="orderId" HeaderText="orderId" SortExpression="orderId"></asp:BoundField>
The last column is:
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="" onclick="LinkButton1_Click" Text="Button"></asp:LinkButton>
as you see , there is "onclick" with some method.. like:
lbltest.Text = gv_order.Rows[gv_order.SelectedIndex].Cells[2].Text;
With that code i get (offcourse) what i have on the selected row in cell number 2. how can i get value from the same row (and from cell number 2) where the button is clicked without the "selceted row" ? example: when i click button on row 2 - i get the cell 2 of that row.
That's possible?
If you want to retrieve 'orderid' in more clean way, you can use CommandName, CommandArgument properties and OnRowCommand event like this:
<asp:GridView (...) OnRowCommand="Gv_RowCommand" (...)> ... <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select" CommandArgument='<%# Bind("orderId") %>' Text="Button"></asp:LinkButton>
and in code behind:
protected void Gv_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Select") { int selectedOrderId = Convert.ToInt32(e.CommandArgument); // ... } }
I hope this is what you want to do.
Edit - my answer to your comment:
Then, it's little more complicated and uses 'selectedRow' in some way. In my own code, I use this approach:
<asp:GridView ID="gv1" (...) DataKeyNames="orderId,email,username" OnRowCommand="Gv_RowCommand" (...)> ... <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select" CommandArgument='<%# DataBinder.Eval(Container,"RowIndex") %>' Text="Button"> </asp:LinkButton>
and in code behind:
protected void Gv_RowCommand(object sender, GridViewCommandEventArgs e) { int selectedRowIndex = Convert.ToInt32(e.CommandArgument); if (e.CommandName == "Select") { int orderId = Convert.ToInt32(gv1.DataKeys[selectedRowIndex]["orderId"]); string email = gv1.DataKeys[selectedRowIndex]["email"].ToString(); // ... } }
这篇关于获取当前的GridView列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!