如何从动态按钮检索值
本文关键字:检索 按钮 动态 | 更新日期: 2023-09-27 18:18:30
我在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。
您可以使用事件处理程序的sender
参数检索对按钮的引用,并将值强制转换为button类型。
在lnk1_Click事件处理程序中,您可以通过将发送者类型强制转换为按钮类型来获取链接并获得链接文本。使用它,您可以检索已完成点击的票号。
像这样:
void lnk1_Click(object sender)
{
Button clickedLinkButton = sender as Button;
String buttonText = clickedLinkButton .Text;
String clickedTicketNumber =
buttonText
.SubString(0, buttonText.IndexOf(':'))
.Trim();"
}
下面是示例代码段
protected void lnk1_Click(object sender, EventArgs e)
{
Button bt = sender as Button;
bt.Text;
}
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.**