按钮并不想调用一个方法(ASP.NET - C#) [英] Button doesn't want to call a method (ASP.NET - C#)

查看:58
本文介绍了按钮并不想调用一个方法(ASP.NET - C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我要感谢您抽出时间来阅读我的问题,也许也帮助我。 :)

first I would like to thank you for taking your time to read my problem and maybe also help me out. :)

现在我的问题...我创建新的按钮与C#code和他们连接到我的小组名为Panel1的:

Now to my issue... I'm creating new buttons with the C# code and attaching them into my Panel named Panel1:

Button novgumb = new Button();
novgumb.Click += new EventHandler(ButtonOdstrani);
novgumb.CommandArgument = "2";
novgumb.Visible = true;
novgumb.Text = "Test";
Panel1.Controls.Add(novgumb);

在这种方式创建的按钮,应该调用我的方法ButtonOdstrani:

The button created in this way, should call my method ButtonOdstrani:

    public void ButtonOdstrani(object sender, EventArgs e)
    {
        string asd = ((Button)sender).CommandArgument;
        //...
    }

现在,当我把按钮code到pageLoad的它工作正常,但是当我把它放在我的方法里面书面方式在网站上的XML,以这种方式创建的按钮就不会调用函数ButtonOdstrani:

Now when I put the Button code into PageLoad it works fine, but when I put it inside my method for writting the XML on the site, the buttons created in this way won't call the function ButtonOdstrani:

protected void IzpisXML()
    {
        BranjeXML();
        string[] element = ime.Split('$');
        //Button gumbs = new Button();
        //gumbs.Click += new EventHandler(ButtonOdstrani);
        //gumbs.Visible = true;
        //gumbs.Text = ("Odstrani ");
        //Panel1.Controls.Add(gumbs);
        for (; z < stevec/2; z++)
        {                    
            string imeGostilne = element[i];
            string naslov = element[i + 1];
            string telefon = element[i + 2];
            string spletnaStran = element[i + 3];
            string odpiralniOD = element[i + 4];
            string odpiralniDO = element[i + 5];
            string boni = element[i + 6];

            Label labela = new Label();

            labela.Text = "<b>Ime gostilne:</b> " + imeGostilne + "<br /><b>Naslov:</b> " + naslov + "<br><b>Telefon:</b> " + telefon + "<br><b>Spletna stran:</b> " + spletnaStran + "<br><b>Odpiralni cas:</b> " + odpiralniOD + " - " + odpiralniDO + "<br /><b>Študnetski boni:</b> " + boni + "<br />";
            labela.Enabled = true;
            labela.EnableTheming = true;
            labela.EnableViewState = true;
            labela.Visible = true;
            labela.ID = ("Label" + (z + 1));
            Panel1.Controls.Add(labela);

            Button novgumb = new Button();
            novgumb.Click += new EventHandler(ButtonOdstrani);
            novgumb.CommandArgument = "2";
            novgumb.Visible = true;
            novgumb.Text = "Test";
            Panel1.Controls.Add(novgumb);

            Label hr = new Label();
            hr.Text = "<hr />";
            hr.Visible = true;
            hr.Enabled = true;
            hr.EnableTheming = true;
            hr.EnableViewState = true;
            Panel1.Controls.Add(hr);
            i += 7;

        }
    }

现在注意外面的评论按钮一句话 - 它没有工作eather方式。
随着它不工作我的意思是,它并没有调用该函数ButtonOdstrani,它只是刷新该网站(回传)。

Now note the commented button outside the for sentence - it didn't work eather way. With "it didn't work" i mean, it didn't call the the function ButtonOdstrani, it just refreshed the site (PostBack).

我真的很感激的任何建议的解决方案。

I would really be thankful for any advice solution.

好了,现在我已经创建了以下内容:

OK, now i've created the the following:

private Button[] ButtonZaOdstranjevanje;

    protected override void OnInit(EventArgs e)
    {
        ButtonZaOdstranjevanje = new Button[stevec/2];
        base.OnInit(e);
        BranjeXML();

        for (int j=0; j < stevec/2; j++)
        {
            Button novgumb = new Button();
            novgumb.Click += new EventHandler(ButtonOdstrani);
            novgumb.Visible = true;
            novgumb.Text = "Odstrani gostilno";
            ButtonZaOdstranjevanje[j] = novgumb;
        }

    }

但问题是,我得到一个错误:索引数组的边界之外
这heppens为:

But the problem is that i get an error: "Index was Outside the bounds of the array" this heppens at:

ButtonZaOdstranjevanje[j] = novgumb;

由于stevec值是4,所以stevec / 2 = 2,且j参数被设置为0这不是合乎逻辑的。
没有人有任何想法如何解决这个问题?

It's not logical since the stevec value is 4, so stevec/2 = 2, and j parameter is set to 0. Does anyone have any idea how to fix this error?

推荐答案

创建动态控件时,你需要在每次回发重新创建。

when creating dynamic controls you need to re-create on each postback.

请参见这里

这篇关于按钮并不想调用一个方法(ASP.NET - C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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