单击回发后如何在gridview中获取动态创建的控件的值 [英] How to get value of the dynamically created control in gridview after click postback

查看:63
本文介绍了单击回发后如何在gridview中获取动态创建的控件的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有Test.aspx和CommonGrid.ascx

Hi Guys,
I Have Test.aspx and CommonGrid.ascx

Test.aspx
<uc:CommonGrid runat="server" .../>


Test.aspx.cs
protected void Page_LoadComplete(object sender, EventArgs e)
{
   //DataBind to gridview
}


CommonGrid.ascx
< asp:GridView ID ="CommonGrid" runat ="server"></asp:GridView>
< asp:Button ID ="Test" runat ="server" Text ="TestClick"/>


CommonGrid.ascx
<asp:GridView ID="CommonGrid" runat="server"></asp:GridView>
<asp:Button ID="Test" runat="server" Text="TestClick"/>

CommonGrid.ascx.cs
protected void Page_Load(object sender, EventArgs e)
{
   //Dynamically add column to grid view
}





protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
   //do other configuration changing cell bgcolor based on cell value etc.
}





protected void Test_Click(object sender, EventArgs e)
{
   //identify checkbox marked as checked. FYI checkbox is one of the control dynamically created inside gridview.
}



问题
当我单击测试"按钮时,它执行回发,然后从Page_Load处理程序重新创建GridView,并执行(CommonGrid.ascx的)Test_Click.当我尝试在Test_Click事件中查找复选框控件时,我似乎找不到它.

问题
如何确保复选框已创建并在单击事件回发后立即保存客户端更新的值?

解决方案 共享我的解决方案,以防其他pipz也遇到此问题,我发现的大部分文章仅讨论了在页面中而不是在usercontrol中动态创建的gridview.
Default.aspx



Problem
When I click on the Test button it perform postback and then the GridView will be recreated from Page_Load handler and Test_Click will be executed (of CommonGrid.ascx). When i tried to find the checkboxes control inside Test_Click event i can''t seem to find it.

Question
How can i make sure that the checkbox is already created and holding the client updated value right after Click event postback?

Solution sharing my solution just incase other pipz also encountered this issue, most of the article i found only discusses dynamically created gridview with in the page and not in usercontrol.
Default.aspx

<uc:GridView ID="TestGridView"  runat="server"/>



Default.aspx.cs 这是技巧,您可以在创建列之后立即执行数据绑定.这实际上是解决方法,可能不介意其余代码.



Default.aspx.cs this is the tricks, you execute databinding right after the creation of the column. this actually is the fix, and may not mind the rest of the code.

protected void Page_Init(object sender, EventArgs e)
    {
        this.TestGridView.Load += this.MyDataBindHandler;
    }

    protected void MyDataBindHandler(object sender, EventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("MyName");

        DataRow dr = table.NewRow();
        dr["MyName"] = "aa";
        table.Rows.Add(dr);

        dr = table.NewRow();
        dr["MyName"] = "bb";
        table.Rows.Add(dr);

        this.TestGridView.BindData(table);
    }



WebUserControl.ascx



WebUserControl.ascx

<asp:Button ID="Button1" runat="server" Text="ClickMe" OnClick="Button1_Click"/>
<asp:GridView ID="CommonGridView" runat="server"></asp:GridView>



WebUserControl.ascx.cs



WebUserControl.ascx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        this.ConfigureGridColumn();
    }

    private void ConfigureGridColumn()
    {
        this.CommonGridView.Columns.Clear();

        //add ordinary column
        BoundField bf = new BoundField();
        bf.DataField = "MyName";
        bf.HeaderText = "Name";

        //add checkbox
        TemplateField tf = new TemplateField();
        tf.ItemTemplate = new CreateCheckBox("chkMyCheckBox");
        tf.HeaderStyle.Wrap = false;
        this.CommonGridView.Columns.Add(tf);

        this.CommonGridView.Columns.Add(bf);
    }

    public void BindData(DataTable dt)
    {
        this.CommonGridView.DataSource = dt;
        this.CommonGridView.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < this.CommonGridView.Rows.Count; i++)
        {
            CheckBox chk = this.CommonGridView.Rows[i].FindControl("chkMyCheckBox") as CheckBox;
            Response.Write(string.Format("Chk: {0}, ", chk.Checked.ToString()));
        }
    }



复选框的模板类



Template class for checkbox

public class CreateCheckBox : ITemplate
{
    string checkboxID;
    public CreateCheckBox(string id)
    {

        this.checkboxID = id;

    }
    public void InstantiateIn(Control container)
    {
        CheckBox chk = new CheckBox();
        chk.ID = checkboxID;

        container.Controls.Add(chk);
    }
}

推荐答案

Test.aspx
Test.aspx
<%@ Register Src="~/WebUserControl.ascx" TagName="GridView" TagPrefix="uc" %>
<uc:GridView ID="TestGridView"  runat="server"/>



Test.aspx.cs
1.用Page_LoadCompleted或任何其他名称替换Page_LoadComplete.唯一的问题是数据绑定一定不能在LoadComplete事件内部.



Test.aspx.cs
1. Replaced Page_LoadComplete with Page_LoadCompleted or any other names. Buttom line is that the databinding must not be inside LoadComplete event.

e.g: protected void MyDataBindHandler(object sender, EventArgs e)
{
// do databindings to gridview
}



2.创建一个Page_Init处理程序,并将MyDataBindHandler附加到usercontrol加载事件.这是为了确保绑定在gridview中添加列之后立即发生.



2. Create a Page_Init handler and attach the MyDataBindHandler to the usercontrol load event. this is to make sure that the binding happens right after adding of columns in gridview.

e.g: protected void Page_Init(object sender, EventArgs e)
{
    this.TestGridView.Load += this.MyDataBindHandler;
}



WebUserControl.ascx



WebUserControl.ascx

<asp:Button ID="Button1" runat="server" Text="ClickMe" OnClick="Button1_Click"/>
<asp:GridView ID="CommonGridView" runat="server"></asp:GridView>




WebUserControl.ascx.cs
1.在Page_Load处理程序中,调用方法以在gridview中添加列.




WebUserControl.ascx.cs
1. In Page_Load handler call the method to add the columns in the gridview

e.g: protected void Page_Load(object sender, EventArgs e)
{
    this.ConfigureGridColumn();
}



2.处理点击事件


顺序是
*一次加载
Default8.aspx Page_Init-> WebUserControl.ascx Page_Load-> Default8.aspx MyDataBindHandler

*在按钮上单击
Default8.aspx Page_Init-> WebUserControl.ascx Page_Load-> Default8.aspx MyDataBindHandler-> Button1_Click

感谢 Vivek Thakur 我得到了以下信息:
如果您在Page_Load事件中创建一个动态控件,并将其添加到PlaceHolder或Panel(打开了视图状态),则即使未在Page_Init()中创建该动态控件,该控件也将保持其状态. >
我已经更新了问题,并添加了带有代码的解决方案部分.请注意,该代码很脏,只是为了解决问题,但可以对其进行改进.



2. Get handle the click event


Sequence is that
* One first load
Default8.aspx Page_Init -> WebUserControl.ascx Page_Load -> Default8.aspx MyDataBindHandler

* On Button click
Default8.aspx Page_Init -> WebUserControl.ascx Page_Load -> Default8.aspx MyDataBindHandler -> Button1_Click

thanks to Vivek Thakuri got this info:
If you create a dynamic control in the Page_Load event, and add it to a PlaceHolder or Panel (with view state turned on), then this dynamic control will maintain its state even though it was not created in the Page_Init().

i have updated the question and added a solution section with codes. note that the code is dirty just to address the issue but can be improved.


在Test_Click方法中使用:
use in Test_Click method:
CheckBox box = (CheckBox)sender;
GridViewRow row = (GridViewRow)box.NamingContainer;



现在您有了单击的行,并且所有控件都应该可用.



Now you have the row that was clicked in and all controls should be available.


重新加载页面后,所有gridview都重新创建了,这就是为什么您无法找到该值..这是您确切的问题吗?

如果是,那么在Page_Load方法内,您编写的任何代码都将放在
它在里面!IsPostBach ..喜欢:

After reloading the page all gridview is getting created again that''s why you are not able to find the value.. This is your exact problem correct?

If yes then inside Page_Load method whatever codes you have written just put
it inside !IsPostBach.. Like:

protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           //Your code goes here...
       }
   }



如果愿意,请接受答案.

祝一切顺利...:)



Accept the answer if you like it..

All the best... :)


这篇关于单击回发后如何在gridview中获取动态创建的控件的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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