点击gridview里的linkbutton,跳转直其他页面。
前台页面:(做个模板列,里面放入LinkButton按钮)
<asp:TemplateField HeaderText="查看/修改">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("que_tpye") %>' CommandName="GetInfo">详细信息</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
后台代码: 在GridView行命令事件中
protected void GridView1_ItemCommand(object sender, GridViewCommandEventArgs e)
{
if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem || e.Item.ItemType == GridItemType.SelectedItem)
{
if (e.CommandName = "GetInfo")
{
Response.Redirect("shitixiangxixinxi.aspx?id=" + Convert.ToInt32(e.CommandArgument));
}
}
}
在shitixiangxixinxi页面 接收id传过来的值就行了
<asp:TemplateField HeaderText="操作" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="btnFavorite" runat="server" OnClick="btnFavorite_OnClick">[收藏]</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
------------------------cs代码----------------------
protected void btnFavorite_OnClick(object sender, EventArgs e)
{
//行号
int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;
Response.Write("[" + row + "]" + this.GridView1.DataKeys[row]["id"].ToString());
Response.Write("[" + row + "]" + this.GridView1.DataKeys[row]["status"].ToString());
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if(row.RowType == DataControlRowType.DataRow)
{
string status = this.GridView1.DataKeys[row.RowIndex]["status"].ToString();
if (status != "1")
{
LinkButton btn = (LinkButton)row.FindControl("btnFavorite");
btn.Enabled = false;
}
}
}
前台代码:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="Grup" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("GrupName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField Text="按钮" />
</Columns>
</asp:GridView>
其中:Grup为我们想使用但不需要显示的列。(如果有多个字段,使用逗号分开)
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack )
{
DataTable dt = new DataTable();
dt.Columns.Add("Grup");
dt.Columns.Add("GrupName");
dt.Rows.Add(new object[] { 0,"营业部" });
dt.Rows.Add(new object[] { 1,"市场部" });
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
// 获取当前行索引
int index = Convert.ToInt32(e.CommandArgument);
// 取出当前行数据键值对象中的值
string strGrup = ((GridView)sender).DataKeys[index].Values["Grup"].ToString();
}
顺便补充一句。
如果你使用模板列中放置按钮控件的方式,要想在按钮事件中获取这种字段值就更简单了。
只需要在按钮的CommandArgument属性设置为想绑定的字段,如:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" CommandArgument=' <%#Eval("Grup") %>' />
</ItemTemplate>
</asp:TemplateField>
按钮事件中如是写:
protected void Button2_Click(object sender, EventArgs e)
{
string strGrup = ((Button)sender).CommandArgument.ToString();
}
总结:调用绑定值的语句:((Button)sender).CommandArgument.ToString()