如何从动态按钮中检索值

时间:2011-12-22 08:53:15

标签: c# asp.net

我在c#代码中创建了一个动态按钮,我在button.text中分配了一些值,但问题是在buttn_Click事件上我想要显示与该值相关的详细信息。所以任何想法如何做到这一点?

protected void Page_Load(object sender, EventArgs e)
{  
    for (int i = 0; i < list.Count; i++)
    {
        lnk1 = new Button();


        VW obj1 = list[i];


        lnk1.Text = "    "+obj1.ticketNo+":      "+obj1.subject+":       "+obj1.qu;
        lnk1.Click += new EventHandler(lnk1_Click);
    }
}

我想在下一页显示上面提到的obj1.ticketno,比如票号:选择了一些值

4 个答案:

答案 0 :(得分:1)

您可以使用事件处理程序的sender参数检索对按钮的引用,并将值转换为Button类型。

答案 1 :(得分:0)

在lnk1_Click事件处理程序中,您可以通过类型将发件人转换为Button类型并获取链接文本来获取链接。使用它,您可以检索已完成点击的票号。

这样的事情:

void lnk1_Click(object sender)
{
    Button clickedLinkButton = sender as Button;
    String buttonText = clickedLinkButton .Text;
    String clickedTicketNumber = 
                                  buttonText
                                  .SubString(0, buttonText.IndexOf(':'))
                                  .Trim();"    
}

答案 2 :(得分:0)

以下是示例代码段

 protected void lnk1_Click(object sender, EventArgs e)
 {
        Button bt = sender as Button;
        bt.Text;
 }

答案 3 :(得分:0)

you can use GridView or Repeater and in Iteme Template you can put button. and bind perticular grid or repeater.

    <asp:repeater runat="server" id="rpt">

    </ItemTemplate>
    <asp:LinkButton runat="serevr" ID="lbtnLInkButton"  CommandArgument='<%#Eval("ID") %>' CommandName="Edit" OnClick="lbtnLInkButton_Click">"+<%#Eval("ticketNo")%>  <%#Eval("subject")%>  <%#Eval("qu")%>
    </ItemTemplate>
    </asp:repeater>


    Bind This Repeater to Datatable or make Dummy DataTable and bind it.


            DataTable dt = new DataTable();
            dt.Columns.Add("ticketNo");
            dt.Columns.Add("Subject");
            dt.Columns.Add("qu");

    for (int i = 0; i < list.Count; i++)
        {

      dt.Rows.Add(new object[] { "Ticket Number Value", "Subject Value", "qu Value"});

    }

    rpt.DataSource = dt;
    rpt.DAtabind();

You can get button event like this 

    protected void lbtnLInkButton_Click(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(((LinkButton)sender).CommandArgument);

        }



***Note : I have writtern the code extempore and not rested it on Visual Studio so there  May be Some Spelling Mistakes.**