本文介绍了如何在动态生成的链接按钮上调用服务器端功能,请单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi ..

我试图在链接按钮单击时调用服务器端函数.这些链接按钮以动态方式生成并将其添加到gridview标头中.即使在链接按钮单击时调用javascript函数,我也能够在网格标头中成功显示按钮,但无法调用服务器端功能.
提供解决方案.
我的代码是

hi..

I am trying to call serverside function on link button click.these link buttons generated dyanamically and add it to gridview header.i am successfully able to display buttons in grid header even calling javascript function on link button click but not able to call server side function.
provide soln.
My code is

Dim lnkSortCol As New LinkButton

           ' Adding link buttons to gridview header

           lnkSortCol.Text = ds.Tables(0).Columns(j).ColumnName
           lnkSortCol.ID = "lnkSortCol_" + j.ToString
           lnkSortCol.ClientIDMode = UI.ClientIDMode.Static
           'lnkSortCol.Attributes.Add("onclick", "sort('" + lnkSortCol.ID + "');return false;")
           AddHandler lnkSortCol.Click, AddressOf sort
           grvCtrl.HeaderRow.Cells(j).Controls.AddAt(0, lnkSortCol)

推荐答案

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    Button button = new Button();
    button.ID = "Button1";
    button.Text = "Button1";
    PlaceHolder1.Controls.Add(button);
    button.Click += new EventHandler(Button1_Click);
}

protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = "Button1_Click";
}



要点是,您需要某种服务器端对象,并将事件处理程序附加到服务器端函数.确保将ID放在动态创建的控件上.



The gist of it is that you need to have some sort of server-side object with an event handler attached to your server-side function. Be sure to put ID''s on your dynamically created controls.


Dim lnkSortCol As New LinkButton
lnkSortCol.Text = ds.Tables(0).Columns(j).ColumnName
lnkSortCol.ID = "lnkSortCol_" + j.ToString
lnkSortCol.CommandArgument = "Any Value That You Want To Pass To your event"
lnkSortCol.Command += New CommandEventHandler(lnkSortCol_Command)

void lnkSortCol_Command(object sender, CommandEventArgs e)
{
            
}


lnkSortCol.attributes.add("OnClick",lnkSortCol_click);

void lnkSortCol_click(object sender, commandEventArgs e)
{

}


这篇关于如何在动态生成的链接按钮上调用服务器端功能,请单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 20:22