<asp:TemplateField HeaderText="下载">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Visible="False">下载</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
明明LinkButton是有ID的,也是runat server的,为什么在后台
调用LinkButton1的话,提示未定义。
我想
LinkButton1.Visible = true来控制它的隐显。
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Visible="False">下载</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
明明LinkButton是有ID的,也是runat server的,为什么在后台
调用LinkButton1的话,提示未定义。
我想
LinkButton1.Visible = true来控制它的隐显。
先调用GV然后FINDCONTROL(控件ID)
<asp:GridView ID="gv" runat="server" OnRowCommand="gv_RowCommand">
<Columns>
<asp:TemplateField HeaderText="下载">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("id") %>' runat="server" Visible="False">下载</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<Columns>
</asp:GridView>protected void gridHotels_RowCommand(object sender, GridViewCommandEventArgs e)
{
long id= ParamUtil.getlong(e.CommandArgument);
response.redirect("http://www.baidu.com/id="+id);
}
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="lk">下载</asp:LinkButton>
后台protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName)
{ case "lk":
LinkButton cb = (LinkButton)this.GridView1.Rows[e.RowIndex].FindControl("LinkButton1");}}自己测试下 应该有小问题 没有环境
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
long id= ParamUtil.getlong(e.CommandArgument);
response.redirect("http://www.baidu.com/id="+id);
}
<asp:TemplateField HeaderText="分配角色权限">
<ItemTemplate>
<asp:HyperLink ID="hlDistribute" runat="server" Text="分配权限" ForeColor="blue"></asp:HyperLink>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
if (e.CommandName == "roleUpdate")
{
string selectRoleId = e.CommandArgument.ToString();//根据关联参数得到角色id值
在行绑定时,即_RowDataBound事件中,为模板列按钮设置CommandName,比如:
LinkButton btn = e.Row.FindControl("btnDo") as LinkButton;
if (btn != null)
{
btn.CommandName = "download";
}
第二步:源代码中
找到模板列的按钮,添加命令参数,手动绑定数据源中的列名
<asp:LinkButton …… CommandArgument='<%# Eval("id") %>' ……/> //注意标点符号
第三步:后台代码
编写_RowCommand事件,当触发的事件名称为search时,获取当前点击的按钮所绑定的命令参数
String key = e.CommandArgument.ToString();
{
LinkButton l=gr.FindControls("LinkButton1") as LinkButton;
}
或Visible='<%# GetVisible(Eval("id"))%>'
public bool GetVisible(object x){return true;};
得用findcontrol找到那个控件
<ItemTemplate>
<asp:LinkButton ID="LinkButton5" runat="server" CommandName="myCmd" CommandArgument='<%# Eval("id") %>'>编辑</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>后台protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "myCmd")
{
int id = Convert.ToInt32(e.CommandArgument);
//你的操作
}
}