为文本框分配值 [英] assign values to textboxes

查看:76
本文介绍了为文本框分配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有十个带有十个ID的文本框.这些都是动态创建的.我需要在按钮单击时为每个文本框提供一些值,
我该如何赋予价值?谁能帮我一下.

预先感谢.

Hi all,

I had ten text boxes with ten ID''s. These are all created dynamically. I need to give some values to each text box on button click,
How can i give values? Can any one help me please.

Thanks in advance.

推荐答案

这样的事情如何:
How about something like this:
protected void Button1_Click ( object sender, EventArgs e )
{
    IEnumerable<TextBox> tboxes = this.Controls.OfType<TextBox> ();
    TextBox txtbx = tboxes.First<TextBox> ( t => t.ID == "txt1" );
    txtbx.Text = "something";
}


请尝试:

在aspx.cs中:
Please try:

In aspx.cs:
public void addButton()
    {
        //setText() is a javasript function to set to 10 textboxes, examples
        /*
         * function setText()
         * {
         *      txt0.value = 'textbox0';
         *      txt1.value = 'textbox1';
         *      txt2.value = 'textbox2';
         *      txt3.value = 'textbox3';
         *      //Etc...
         * }
         */
        Response.Write("<input type="button"  önclick="setText();" value="Click Me" /> <br />");
    }
    
    public void addTextBoxes()
    {        
        for (int i = 0; i < 10; i++)
        {
            Response.Write("<input id="txt" + i.ToString() + "" type="textbox" /> <br />");
        }        
    }


在aspx中:


In aspx:

<head  runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
        function setText(){
            form1.txt0.value = ''textbox0'';
            form1.txt1.value = ''textbox1'';
        }
    </script>
</head>
<body>
    <form id="form1"  runat="server">
        <%addButton(); %> 
        <%addTextBoxes(); %>
    </form>
</body>


问候.

安迪


Regards.

Andy


[textboxID] .Text ="whatever";

将其放在按钮单击事件处理程序中.


没有看到ASP.Net标记. :doh:

由于这些是动态创建的控件,因此AFAIK您必须在回发时重新创建它们.重新创建时,您可以检查回发是否是由于单击按钮引起的,如果是,则设置文本.
[textboxID].Text = "whatever";

Put this in button click event handler.


Did not saw ASP.Net tag. :doh:

Since these are dynamically created controls, AFAIK you will have to recreate them on post back. While recreating, you can check if the postback is because of button click and if yes, set the text.


这篇关于为文本框分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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