我想在rowcommand 事件中获取该行的datakeys[]值。因为需要获取的值不止一个,不能用e.commandargument绑定,不只应该怎样获取?
解决方案 »
- 后台动态添加字段,前台进行插入
- 关于Session的问题
- 如何调用fileupload上传的文件
- Gridview更新时如何获取EditItemTemplate中的值做参数
- 如何将<font color=000000>标题:</font>替换成 "标题"
- 请教JS的a>>>b与C#中的a>>b
- SMTP技术发送电子邮件
- @@@@@如何得到一个表内某个行的某字段值???比如得到login表内的第5行的ID的取值?
- 如何在Page_load()中用C#来显示系统日期?(在线等待)
- asp.net的一个问题,我头疼了好几天了
- 怎么在 Repeater或者用DataList 数据的实现分层帮定
- 如何打包asp.net
GridView1.SelectedDataKey.Values[0]
在后台代码中
protected void gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "view")
{ }
}
在后台代码中
protected void gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string apply;
if(e.CommandName == "view")
{
apply = gridview1.SelectedDataKey.Values[0].ToString();
state = gridview1.SelectedDataKey.Values[1].ToString();
}
}
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "cart" Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument.ToString().Trim())
Dim label1 As Label = CType(Me.GridView1.Rows(index).FindControl("lblproid"), Label)
Dim id As Integer = CType(label1.Text.ToString, Integer)
Dim label2 As Label = CType(Me.GridView1.Rows(index).FindControl("lblproname"), Label)
Dim produtname As String = label2.Text.ToString
Dim price As Decimal = CDec(Me.GridView1.Rows(index).Cells(4).Text.ToString)
MsgBox(id)
MsgBox(produtname)
MsgBox(price)
Profile.Cart.AddItem(id, produtname, price)
MsgBox("你已成功将商品加入购物车!")
Else
Dim index As Integer = Convert.ToInt32(e.CommandArgument.ToString().Trim())
Dim label1 As Label = CType(Me.GridView1.Rows(index).FindControl("lblproid"), Label)
Dim id As Integer = CType(label1.Text.ToString, Integer)
Profile.Favorites.AddProduct(id)
MsgBox("放入衣柜")
End If
End Sub Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim bt1 As Button = CType(e.Row.FindControl("cart"), Button)
bt1.CommandArgument = e.Row.RowIndex.ToString
Dim bt2 As Button = CType(e.Row.FindControl("favorite"), Button)
bt2.CommandArgument = e.Row.RowIndex.ToString
End If
End Sub不知道对你有没有帮助
{
int rowIndex = Int32.Parse((String)e.CommandArgument);
Response.Write(GridView1.DataKeys[rowIndex].Values[1]);
}
((String)e.CommandArgument为null
{
int rowIndex = Int32.Parse((String)e.CommandArgument);
Response.Write(GridView1.DataKeys[rowIndex].Values[1]);
}
SelectCommand="SELECT [userid], [username], [password], [roleid], [realname] FROM [tusers]">
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="userid,roleid"
DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand" HorizontalAlign="Center" AllowPaging="True" AllowSorting="True">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="userid" HeaderText="userid" InsertVisible="False" ReadOnly="True"
SortExpression="userid" />
<asp:BoundField DataField="username" HeaderText="username" SortExpression="username" />
<asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
<asp:BoundField DataField="roleid" HeaderText="roleid" SortExpression="roleid" />
<asp:BoundField DataField="realname" HeaderText="realname" SortExpression="realname" />
</Columns>
</asp:GridView>
我的后台代码:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex = Int32.Parse((String)e.CommandArgument);
Response.Write(GridView1.DataKeys[rowIndex].Values[0]);
Response.Write(GridView1.DataKeys[rowIndex].Values[1]);
}
前台的测试页面:
<asp:GridView ID="gridview1" runat="server" DataKeyNames="id,name" AutoGenerateColumns="False" OnRowCommand="gridview1_RowCommand" >
<Columns>
<asp:TemplateField HeaderText="测试">
<ItemTemplate>
<table style="width:540px; height:170px">
<tr>
<td align="left" style="height:8px">
<asp:Label ID="lblId" runat="server"><%#Eval("id") %></asp:Label></td>
</tr>
<tr>
<td>
<asp:Label ID="lblName" runat="server"><%#Eval("name")%></asp:Label></td>
</tr>
<tr>
<td>
<asp:LinkButton ID="lbtnTest" runat="server" CommandName="view">查看</asp:LinkButton></td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
结帖。