RowCommand没有发射 [英] RowCommand not firing

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

问题描述

我想以编程方式 ASP.NET C#添加一个按钮作为后端。我能够显示的按钮,但是当用户点击该按钮的RowCommand不被炒鱿鱼。

我认为问题是,我创建一个提交按钮,用户点击后按钮。如果我做在Page_Load的按钮,然后RowCommand工作。

这是网站应该如何流动:


  1. 页面加载:显示基本数据

  2. 在一个提交按钮用户点击它创建了一个 GridView控件,其中有一个 ButtonField字段。无论是 GridView控件 ButtonField字段获得后端动态创建。

  3. 然后用户点击按钮生成的,它应该解雇 RowCommand

下面是code:

的Page_Load

 保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
     //没有因为我们只希望用户后所创建按钮
     在提交//点击
    }

randomGridView

生成GridView的和按钮

 公共无效randomGridView(表笔,INT X)
    {        GridView控件GV1 =新的GridView();
        GridView控件GV2 =新的GridView();
        GridView控件GV3 =新的GridView();
        GridView控件GV4 =新的GridView();        的TableRow R =新的TableRow();
        的for(int i = 0; I< X,我++)
        {
            TableCell的C =新的TableCell();
            c.BorderStyle = BorderStyle.Solid;
            如果(我== 0)
            {
                c.Controls.Add(GV1);
            }
            如果(ⅰ== 1)
            {
                c.Controls.Add(GV2);
            }
            如果(ⅰ== 2)
            {
                c.Controls.Add(GV3);
            }
            如果(我== 3)
            {
                c.Controls.Add(GV4);
            }
            r.Cells.Add(C);
        }
        t.Rows.Add(R);        //在XML被结合到数据格
        XDS的XmlDataSource =新的XmlDataSource();        xds.Data = XML;
        xds.DataBind();
        xds.EnableCaching = FALSE;        gv1.DataSource = XDS;        ButtonField字段临时=新ButtonField字段();
        temp.ButtonType = ButtonType.Image;
        temp.ImageUrl =〜/ checkdailyinventory.bmp
        temp.CommandName =buttonClicked;
        temp.HeaderText =;
        gv1.Columns.Add(临时);        gv1.RowCommand + =新GridViewCommandEventHandler(CustomersGridView_RowCommand);
        gv1.RowDataBound + =新GridViewRowEventHandler(inventoryGridView_RowDataBound);
        gv1.DataBind();        xds.Data = XML;
        xds.DataBind();
        xds.EnableCaching = FALSE;        gv2.DataSource = XDS;
        gv2.DataBind();
        xds.Data = XML;
        xds.DataBind();
        xds.EnableCaching = FALSE;        gv3.DataSource = XDS;
        gv3.DataBind();        xds.Data = XML;
        xds.DataBind();
        xds.EnableCaching = FALSE;        gv4.DataSource = XDS;
        gv4.DataBind();
    }

inventoryGridView_RowDataBound

在哪里我试着manaully添加一个按钮,第一个单元格

 公共无效inventoryGridView_RowDataBound(对象发件人,GridViewRowEventArgs E)
    {
        按钮临时=新按钮();
        temp.CommandName =buttonClicked;
        temp.Text =临时按钮!;
        e.Row.Cells [0] .Controls.Add(临时);
    }

CustomersGridView_RowCommand

这是需要被解雇是什么

 公共无效CustomersGridView_RowCommand(对象发件人,GridViewCommandEventArgs E)
    {
        如果(e.CommandName ==buttonClicked)
        {
            INT指数= Convert.ToInt32(e.CommandArgument);
            Label1.Text = index.ToString();        }
    }

的button1_Click

什么实际创建GridView的和按钮

 公共无效的button1_Click(对象发件人,EventArgs的发送)
    {
        randomGridView(表1,1);
        randomGridView(表2,4);
    }

那么,如何让按钮发射 CustomersGridView_RowCommand ?或者是不可能的链接动态生成的按钮?

编辑:这里是aspx页面:

 <表>
            &所述; TR>
                &所述; TD>
                    < D​​IV ID =DIV1的风格=WIDTH:257px;高度:500像素;溢出-X:滚动;溢出-Y:隐藏;>
                        < ASP:表ID =表1=服务器>
                        < / ASP:表>
                    < / DIV>
                < / TD>
                &所述; TD>
                    < D​​IV ID =DIV2的风格=宽度:500像素,高度:500像素;溢出:滚动;>
                        < ASP:表ID =表2=服务器>
                        < / ASP:表>
                    < / DIV>
                < / TD>
            < / TR>
        < /表>


解决方案

我终于能拿到RowCommand后动态创建它开火。这里是我的解决方案,虽然我不能说这是做事的最佳方式:

 保护覆盖无效的OnInit(EventArgs的发送)
    {
        base.OnInit(E);        如果(的IsPostBack){
            如果(的Request.Form [Button1.UniqueID]!= NULL)
            {
                //从服务器获取数据
                randomGridView(表1,1);
                randomGridView(表2,4);
            }
            //这里只是显示表单        }
    }

现在,而不是等到我到了button_click事件我OnInit的检查,如果被点击的按钮。如果这是后来我才知道用户要我加载数据并显示它。目前在code以上我只是做和在同一个函数显示的数据(这是一个糟糕的设计,但我只是简单的原型)。

然后,每当用户点击任何将导致回传会再表添加到控件。然后,可以控制火(我不知道为什么或者怎么样,但是它的工作原理。)

只是为了表明我们甚至不需要一个按钮的功能:

 公共无效的button1_Click(对象发件人,EventArgs的发送)
    {
        // randomGridView(表1,1);
        // randomGridView(表2,4);
        //Session.Add(\"doIt\",true);
    }

此外,这可能是不做事的最佳方式,但它的作品。如果有人有更好的解决方案,请让我知道。

I am trying to programmatically add a button in ASP.NET with C# as the backend. I am able to display the button, but the RowCommand isn't getting fired when the user clicks the button.

I believe the problem is that I am creating the buttons after the user clicks on a "Submit" button. If I make the button in the page_load then the RowCommand works.

This is how the website should flow:

  1. Page load: display basic data.
  2. User clicks on a Submit button which creates a GridView, which has a ButtonField. Both the GridView and the ButtonField get created dynamically on the backend.
  3. User then clicks on the generated Button and it should fire the RowCommand

Here is the code:

Page_load

protected void Page_Load(object sender, EventArgs e)
    {
     //Nothing because we only want to create the button after the user
     //clicks on submit   
    }

randomGridView

Generates the Gridviews and buttons

public void randomGridView(Table t, int x)
    {

        GridView gv1 = new GridView();
        GridView gv2 = new GridView();
        GridView gv3 = new GridView();
        GridView gv4 = new GridView();

        TableRow r = new TableRow();
        for (int i = 0; i < x; i++)
        {
            TableCell c = new TableCell();
            c.BorderStyle = BorderStyle.Solid;
            if (i == 0)
            {
                c.Controls.Add(gv1);
            }
            if (i == 1)
            {
                c.Controls.Add(gv2);
            }
            if (i == 2)
            {
                c.Controls.Add(gv3);
            }
            if (i == 3)
            {
                c.Controls.Add(gv4);
            }
            r.Cells.Add(c);
        }
        t.Rows.Add(r);

        //Where the xml gets bonded to the data grid
        XmlDataSource xds = new XmlDataSource();

        xds.Data = xml;
        xds.DataBind();
        xds.EnableCaching = false;

        gv1.DataSource = xds;

        ButtonField temp = new ButtonField();
        temp.ButtonType = ButtonType.Image;
        temp.ImageUrl = "~/checkdailyinventory.bmp";
        temp.CommandName = "buttonClicked";
        temp.HeaderText = " ";
        gv1.Columns.Add(temp);

        gv1.RowCommand += new GridViewCommandEventHandler(CustomersGridView_RowCommand);
        gv1.RowDataBound += new GridViewRowEventHandler(inventoryGridView_RowDataBound);
        gv1.DataBind();

        xds.Data = xml;
        xds.DataBind();
        xds.EnableCaching = false;

        gv2.DataSource = xds;
        gv2.DataBind();


        xds.Data = xml;
        xds.DataBind();
        xds.EnableCaching = false;

        gv3.DataSource = xds;
        gv3.DataBind();

        xds.Data = xml;
        xds.DataBind();
        xds.EnableCaching = false;

        gv4.DataSource = xds;
        gv4.DataBind();
    }

inventoryGridView_RowDataBound

Where I tried manaully adding a button to the first cell

public void inventoryGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Button temp = new Button();
        temp.CommandName = "buttonClicked";
        temp.Text = "temp button!";
        e.Row.Cells[0].Controls.Add(temp);
    }

CustomersGridView_RowCommand

This is what needs to get fired

public void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "buttonClicked")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            Label1.Text = index.ToString();

        }
    }

Button1_Click

What actually creates the gridviews and the buttons

public void Button1_Click(object sender, EventArgs e)
    {
        randomGridView(Table1, 1);
        randomGridView(Table2, 4);
    }

So how do I get the button to fire CustomersGridView_RowCommand? Or is it impossible to link a dynamically generated button?

Edit: Here is the ASPX Page:

        <table>
            <tr>
                <td>
                    <div id="div1" style="width: 257px; height: 500px; overflow-x: scroll; overflow-y: hidden;">
                        <asp:Table ID="Table1" runat="server">
                        </asp:Table>
                    </div>
                </td>
                <td>
                    <div id="div2" style="width: 500px; height: 500px; overflow: scroll;">
                        <asp:Table ID="Table2" runat="server">
                        </asp:Table>
                    </div>
                </td>
            </tr>
        </table>

解决方案

I was finally able to get the RowCommand to fire after dynamically creating it. Here is my solution although I cannot say this is the best way of doing things:

protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        if (IsPostBack){
            if (Request.Form[Button1.UniqueID] != null)
            {
                //Get the data from the server
                randomGridView(Table1, 1);
                randomGridView(Table2, 4);
            }
            //Just display the form here



        }
    }

Now instead of waiting till I get to the button_click event I check in OnInit if the button was clicked. If it was then I know the user wants me to load the data and display it. Currently in the code above I just make and display data in the same function (Which is a bad design, but I was just simply prototyping).

Then whenever the user clicks on anything that will cause a postback it will then add the table to the controls. Then the controls can fire (I have no idea why or how, but it works..)

Just to show that we don't even need a button function:

        public void Button1_Click(object sender, EventArgs e)
    {
        //randomGridView(Table1, 1);
        //randomGridView(Table2, 4);
        //Session.Add("doIt",true);
    }

Again this probably isn't the best way of doing things, but it works. If someone has a better solution please let me know.

这篇关于RowCommand没有发射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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