如何动态地将值分配给Asp.Net中的按钮? [英] How Do I Assign The Value Dynamically To The Button In Asp.Net?

查看:77
本文介绍了如何动态地将值分配给Asp.Net中的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,





我的网页上有100个按钮..

im使用以下代码

Hi All,


I have load 100 buttons in my web page..
im using below code

<%
int x=1;
for(int i=0;i<10;i++)
{
   for(int j=0;j<10;j++
   {%>
      <asp:Button ID="Button1"  runat="server"  önclick="Button1_Click" Text='<%=x%>' />
      x=x+1;
   <%}
}%>



Q1如何将x值分配到文本中;

Q2。如何为每个按钮创建单独的功能。



请帮帮我新的ASP.net


Q1. How to assign the x value into the text;
Q2. How to create separate function for each button.

Please help me im new for the ASP.net

推荐答案

嗨...... rajasekaran



有两个解决这个问题的方法。



1.使用以下代码



hi... rajasekaran

there are two solutions to this problems.

1. use the following code

<%
int x=1;
for(int i=0;i<10;i++)
{
   for(int j=0;j<10;j++
      {
      button.text = x;
      }
      x=x+1;
}%>







2.使用datalist或gridview,您可以动态地将文本绑定到多个按钮




2. Use datalist or gridview where you can bind the text to your multiple buttons dynamically


您不需要多个事件。只创建一个方法来处理按钮事件:

You don't need multiple events. Create only one method to handle the buttons event:
önclick="MyButton_Click"



创建一个新的按钮控件,它继承Button并添加一个新属性MyID to识别按钮。您还可以使用其他属性来识别按钮,如文本,但新属性是一种更优雅的解决方案。



使用此代码处理不同的按钮事件:


Create a new button control which inherits the Button and add a new property MyID to identify the button. You can also use another property to identify the button, like Text, but a new property is a more elegant solution.

Use this code to handle distinct button events:

private void MyButton_Click(object sender, System.EventArgs e)
{
	NewButton newButton = (NewButton)sender;

	switch(newButton.MyID) { 
    		//Code to handle button click
	}
}





您还可以创建一个分配Text值的方法。使用MyID属性来标识按钮。



You also create a method that assigns the Text values. Use MyID property to identify the buttons.


这篇关于如何动态地将值分配给Asp.Net中的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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