本文介绍了将数据附加到AspButton控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有ID的文件(我需要)。我想动态创建一个asp:Button控件,点击后会触发一个事件,然后将页面导航到另一个URL,查询字符串为id = [documentID]



所以我如何将数据(documentId)附加到动态创建的按钮,所以当它点击这个信息时可以从触发的事件中访问?这样做的任何其他方式也很感激,我需要在C#中执行此操作,通常我会在Javascript中完成所有操作,所以我有点新。



非常感谢

I have a document, with an ID (which i need). i am wanting to dynamically create a asp:Button control, that when clicked will trigger an event to then navigate the page to another URL with a query string of id= [documentID]

so how do i attach data (the documentId) to the dynamically created button, so when it clicks this information can be accessed from the event triggered? Any other ways of doing this are grateful too, i need to do this in C#, and normally i'd do it all in Javascript, so i'm a bit new.

Many Thanks

推荐答案

protected void Page_Load(object sender, EventArgs e)
       {
           Button b = new Button();
           b.ID = "btnTest";
           b.CommandArgument = "42";
           b.Click += new EventHandler(btnTest_Click);
           b.Text = "clickme";
           placeholder.Controls.Add(b);
       }

       protected void btnTest_Click(object sender, EventArgs e)
       {
           var button = (Button)sender;
           throw new Exception(button.CommandArgument.ToString());
       }


这篇关于将数据附加到AspButton控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 19:38