如何创建一个asp:在C#编程按钮后面(不点击处理) [英] How to create an asp:button programmatically in c# behind (without click handling)

查看:210
本文介绍了如何创建一个asp:在C#编程按钮后面(不点击处理)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的按钮提供我所需要的确切功能,但它必须以编程做,因为我将作出一个以上的基于一些变数,但后者我可以计算出,如果我可以只创建下面的按钮作为首发

 < ASP:按钮=服务器按需=Load_ItemsCommandArgument =1文本=提交/>

这是我一直在做的更多信息<一个href=\"http://stackoverflow.com/questions/17884044/method-being-triggered-on-page-load-but-not-via-asp-button/\">here.

编辑:

我看过的问题这样,但看不到哪里去,从按钮btnSave =新按钮(); 来看到网页上的按钮。另外,我发现大多数问题似乎去使用C#来处理一下,但我想有一个按需属性来处理它。

如果我执行下列操作,并使用开发工具BTN搜索,我没有得到任何结果。

 按钮btnSave =新按钮();
btnSave.ID =btnSave;
btnSave.Text =保存;
btnSave.CssClass =BTN;


解决方案

下面是你如何创建Command事件的按钮。

一旦被创建,你需要找到显示它的方式。下面的示例使用占位符;你也可以用替换面板如果你想。

ASPX

 &LT; ASP:占位符=服务器ID =PLACEHOLDER1&GT;&LT; / ASP:占位符&GT;

code

的背后

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    的for(int i = 0;我小于5;我++)
    {
        VAR按钮=新的Button
        {
            ID =按钮+ I,
            CommandArgument = i.ToString(),
            文本=提交+ I
        };
        button.Command + = Load_Items;
        PlaceHolder1.Controls.Add(按钮);
    }
}私人无效Load_Items(对象发件人,CommandEventArgs E)
{
    INT ID = Convert.ToInt32(e.CommandArgument);
    //使用ID的东西
}

The following button provides the exact functionality I require, but it must be done programmatically as I will be making more than one based on some variables, but the latter I can figure out if I can just create the button below as a starter:

<asp:Button runat="server" OnCommand="Load_Items" CommandArgument="1" text="Submit" />

More information on what I've been doing here.

Edit:

I have looked at questions like this, but don't see where to go from Button btnSave = new Button(); to seeing a button on the page. Also, most questions I've found seem to go about using C# to handle clicking, but I want to have an OnCommand property to handle it.

If I do the following and search using the developer tools for "btn", I get no results.

Button btnSave = new Button();
btnSave.ID = "btnSave";
btnSave.Text = "Save";
btnSave.CssClass = "btn";

解决方案

Here is how you create a Button with Command event.

Once it is created, you need to find a way of displaying it. The following example uses PlaceHolder; you can also substitute with Panel if you want.

ASPX

<asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>

Code Behind

protected void Page_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 5; i++)
    {
        var button = new Button
        {
            ID = "Button" + i,
            CommandArgument = i.ToString(),
            Text = "Submit" + i
        };
        button.Command += Load_Items;
        PlaceHolder1.Controls.Add(button);
    }
}

private void Load_Items(object sender, CommandEventArgs e)
{
    int id = Convert.ToInt32(e.CommandArgument);
    // Do something with id
}

这篇关于如何创建一个asp:在C#编程按钮后面(不点击处理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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