GridView中的动态链接按钮出现问题 [英] Problem in dynamic link button in GridView

查看:48
本文介绍了GridView中的动态链接按钮出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我动态添加链接按钮到gridview。



LinkBut​​ton av =新的LinkBut​​ton();



关于Page_init()

  protected   void  Page_Init( object  sender,EventArgs e)
{
av.ID = lnkAssetView;
av.Text = LinkBut​​ton;
av.Width = 15 ;
av.Click + = new EventHandler(lnk_Click);
}





LinButton的EventHandler

  protected   void  lnk_Click( object  sender,EventArgs e)
{
// 我的代码
}





Gridview OnRowDataBound



  protected   void  OnRowDataBound( object  sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells [col]。 Controls.Add被(AV);
}
}



当我点击LinkBut​​ton时,回发没有发生,linkbutton消失了。

请帮我解决这个问题。

提前谢谢

解决方案

无需生成单击Link for Linkbutton的事件只需将LinkBut​​ton的CommandName属性设置为'MyCommand'并使用GridView的RowCommand事件。

在RowCommand Event中编写以下代码以检查链接按钮是否被点击。

 if(e.CommandName ==MyCommand)
{
//在这里执行操作
}


Hai

为什么你在网格视图中添加链接按钮dynamicall,为什么你不试图为一个字段添加链接按钮,就像你在网格视图中有3个列,如ID,名称和地址。在名称列中,您可以添加链接按钮。文本框使用显示值和链接按钮用于编辑用户进一步处理,尝试我的代码

 <   asp:TemplateField     HeaderText   = 名称 >  
< EditItemTemplate >
< span class =code-keyword>< asp:TextBox ID = TextBox1 runat = server 文字 =' < ;% #Bind( 名称)%>' < span class =code-keyword>> < / asp:TextBox < span class =code-keyword>>
< / EditItemTemplate >
< ItemTemplate >
< asp: LinkBut​​ton ID = LinkBut​​ton4 runa t = server CommandArgument =' <% #Bind( UserId)%>' CommandName = EditUser 文字 =' <%#绑定( 名称)%>' > LinkBut​​ton < / asp:LinkBut​​ton >
< / ItemTemplate >
< HeaderStyle Horizo​​ntalAlign = / >
< ItemStyle Horizo​​ntalAlign = / >
< / asp:TemplateField >







< pre lang =vb> 受保护的 Sub gvAll_RowCommand( ByVal 发​​件人作为 对象 ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)句柄 gvAll.RowCommand
< span class =code-keyword>如果 e.CommandName = EditUser < span class =code-keyword>然后
' Ur操作编码
结束 如果
结束 Sub


以上网格视图代码中的
将为名称字段添加链接按钮,因此它显示带有链接按钮的名称,如果你点击名称它作为链接按钮。



注意:你没有提及使用链接按钮,删除,编辑?在我的例如用于网格视图中的编辑用户,如果你想删除你需要使用单独的链接按钮。请提及你使用的目的是什么?



问候

Aravind


Hi,

I am adding link button dynamically to a gridview.

LinkButton av = new LinkButton();

On Page_init()

protected void Page_Init(object sender, EventArgs e)
        {
            av.ID = "lnkAssetView";
            av.Text = "LinkButton";
            av.Width = 15;
            av.Click += new EventHandler(lnk_Click);
        }



EventHandler for LinButton

protected void lnk_Click(object sender, EventArgs e)
{
 // My code
}



Gridview OnRowDataBound

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
            e.Row.Cells[col].Controls.Add(av);
      }
}


When I click on LinkButton, postback is not happening, and linkbutton got disappears.
Please help me how to fix this.
Thanks in advance

解决方案

There is no need to generate Click Event for Linkbutton just Set CommandName property of your LinkButton as 'MyCommand' and use RowCommand Event of GridView.
write following code in RowCommand Event to check whether the link button is clicked or not.

   if(e.CommandName == "MyCommand")
{
      //do the operation here
}


Hai
Why u add link button dynamicall in grid view,why u not try to add link button for one field like if u have 3 colums in grid view like ID,Name and Address. in Name column u can add link button. Textbox used show value and link button used for edit users for further process, try my code

  <asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton4" runat="server" CommandArgument='<%#Bind("UserId")%>'  CommandName="EditUser" Text='<%# Bind("Name") %>'>LinkButton</asp:LinkButton>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>




Protected Sub gvAll_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvAll.RowCommand
        If e.CommandName = "EditUser" Then
'Ur operation codings
End If
    End Sub


in above grid view code i will add link button for name field,so it show Name with link button,if u click name it act as link button.

Note : u not mention for use of link button,for delete,edit ? in my eg used for edit users in grid view,if u want delete u need to use separate link button.so pls mention for what purpose u use ?

Regards
Aravind


这篇关于GridView中的动态链接按钮出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆