GridView, Edit Row, user control button

I succesfully put a custom control inside a placeholder inside a cell when editing a row but when i click on a button or do anything which causes a postback the control disappears.

protected void DropDownList1_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow editrow = GridView1.Rows[GridView1.EditIndex];
        PlaceHolder p1 = (PlaceHolder)editrow.FindControl("OptionsPlaceHolder");

        DropDownList ddl1 = (DropDownList)sender;
        if (ddl1.SelectedItem.Text == "Combo")
        {
            Control uc1 = LoadControl("~/UserControls/OptionList.ascx");
            p1.Controls.Add(uc1);
        }
    }
 
0
InsanePaul
2/10/2009 1:47:00 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

4 Replies
753 Views

Similar Articles

[PageSpeed] 19
Get it on Google Play
Get it on Apple App Store

Once you add the dynamic control, you must somehow re-create it each and every postback. If possible, it might be better to simply declaratively add your control to the GridViewRow and simply hide or display it based on your DropDownList selection.

There are many articles on the web with regards to using dynamically generated controls.


Thanks, Ed

Microsoft MVP - ASP/ASP.NET

0
ecbruck
2/10/2009 1:58:29 PM

Controls added dynamizally have to be added on every postback.  Usually this type of adding I've done in Page_Load so that it's done every time the page posts back.  Hope this helps.


C# <---> VB.Net Translator
0
MetalAsp
2/10/2009 1:59:31 PM

ecbruck:

Once you add the dynamic control, you must somehow re-create it each and every postback. If possible, it might be better to simply declaratively add your control to the GridViewRow and simply hide or display it based on your DropDownList selection.There are many articles on the web with regards to using dynamically generated controls.

That was very helpful. I've added it below. I show the placeholder containing the usercontrol when the user is in edit mode and selects "Combo" from a dropdownlist in another column.

            <asp:TemplateField HeaderText="Name" SortExpression="Name">
                <EditItemTemplate>
                    <asp:Label ID="lbEditOptionName" Visible="true" runat="server" Width="150px" Text='<%# Bind("Name")%>'></asp:Label> 
                    <asp:PlaceHolder ID="OptionsPlaceHolder" runat="server" Visible="false" >
                        <UserControl:ListBox ID="ucList" runat="server" />                    
                    </asp:PlaceHolder>
                </EditItemTemplate>
But I can't seem to show the placeholder if the selected item was already "Combo". I've tried several methods, actually stumbling on one of your old posts which I've amended slightly. The rowediting event fires before the controls are generated therefore I refresh the data then retrieve the controls. But I can't do anything with the controls eg. I can't change lb1.Text and I can't make the placeholder visible. So rather than use the RowEditing event I tried using the (2) the RowCreated event which doesn't read the selecteditem of the dropdownlist.

1.

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        // Refresh data to set GridView into Edit mode
        GridView1.DataBind();

        // Retrieve row
        GridViewRow editRow = GridView1.Rows[GridView1.EditIndex];      

        // Retrieve controls
        PlaceHolder p1 = (PlaceHolder)editRow.FindControl("OptionsPlaceHolder");
        DropDownList ddl1 = (DropDownList)editRow.FindControl("DropDownList1");
        Label lb1 = (Label)editRow.FindControl("lbEditOptionName");

        if (ddl1.SelectedItem.Text == "Combo")
        {
            p1.Visible = true;
            lb1.Text = "ddddd";
        }
        else
        {
            p1.Visible = false;
        }
    }

 2.

    protected void GridView_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowState == DataControlRowState.Edit)
        {
            //// Retrieve control
            //PlaceHolder p1 = (PlaceHolder)e.Row.FindControl("OptionsPlaceHolder");
            //DropDownList ddl1 = (DropDownList)e.Row.FindControl("DropDownList1");
            //Label lb1 = (Label)e.Row.FindControl("lbEditOptionName");

            //if (ddl1.SelectedItem.Text == "Combo")
            //{
            //    p1.Visible = true;
            //    lb1.Text = "ddddd";

            //}
            //else
            //{
            //    p1.Visible = false;
            //}
        }
    }
 
0
InsanePaul
2/11/2009 10:36:58 AM

Try the RowDataBound event handler instead, and remember that RowState must be checked like so: 

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
	GridViewRow row = e.Row;

	if ((row.RowType == DataControlRowType.DataRow) && ((row.RowState & DataControlRowState.Edit) > 0))
	{
        DropDownList ddl = row.FindControl("DropDownList1") as DropDownList;

        if (ddl != null)
        {

        }
	}
}

Thanks, Ed

Microsoft MVP - ASP/ASP.NET

0
ecbruck
2/11/2009 1:13:59 PM
Reply:

Similar Artilces:

Add new controls to a gridview row; when editing that row using the edit row feature in the gridview control.
Hi all  I have a questions that looks hard / complex to me, and i know you can help me in this. Q {  I have row in a Gridview and i have an edit option to it. I press the edit button, to edit the values in that row and update data all the way back to the database. Now when i press the edit button, i want a drop down list to be displayed that contain values for a particular field in my grid view. (for now only plain text boxes appear.) } I hope you understand my question, all i need to do is replace the text box controls with a dropdown list when i am editing a row in ...

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: http://forums.asp.net/t/1069245.aspx Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

Dynamically loading user control to a gridview
Hello,I know there are many posts about loading user control dynamically and events which not fire. I tried  everything suggested but nothing helped. The difference in my example is that I am loading the user control into a specific line in a grid view after the user clicked on a button.So, the containing object in this case is DataControlFieldCell but it is a difference one on every click of the user.I tried to to set the ID of the user control on the page load of the control (this.ID = "MyControl1";) and I am loading the control on post back in the page_load of the parent pa...

How to call user control procedure from button not in user control
Hi,I have a procedure in the code behind of my user control, e.g. PopulateGrid(). What I want to do is to call this procedure from a button that is not in the user control, but in the web form (.aspx). Clearly, if the button is included in the user control, then there's no problem because I can call the procedure by writing PopulateGrid().Any ideas on how to do this? Thanks,Xiangjiao I finally figured it out. I have solved my problem by creating an instance of my user control on the web form and then calling myUserControl.PopulateGrid(). ...

User control in a user control
Hello all, I have created a user control with some checkboxes, and public properties to get the values of these checkboxes. I have also created a second user control using the first user control; this second control also access the public properties of the first one. My problem is when I implement the second control in an aspx page, if I click at runtime the checkboxes in the first control, I am able to access its public properties from the methods of the second control but the values of these properties are wrong. while at design time, I do not have this problem. Than...

user control to user control
I have an aspx page that have a "Register" directive to two usercontrol called rptMaster.ascx and rptDetail.ascx. I'm trying to set up a Master-Detail relationship and wish for the rptMaster.ascx to trigger and event from a dropdownlist and for the rptDetail to be listening for DropDownList event. I believe I have gotten the events part okay with help from this articel http://codeproject.com/aspnet/Page_UserControl.asp but my declaration for a private statement to my rptMaster class is producing an error. I'm using beta 2 of asp.net and I 'm having trouble getting a reference TO ...

User Controls in User Controls?
I have the follow set up and it is throwing an error "Unhandled error loading module" ... is there some trick to this that I don't know?I have a project A that uses controls from project B (project B is just a project with all my user controls used through out other modules).  project A uses user control 1 from project B with out any issues at all - great.  But then I wanted to add user control 2 to user control 1 and that is when I get my error.  In fact I don't even have the user control 2 in user control 1, just when I put the register part into user control 1 is when it al...

User Control in a User Control
I have a first user control that contain a second user control. If I cahe the first user control can I still dynamically control the second one or it is cached with the first one therefore can not control it anymore? Thanks When your first control is cached, its rendered out and its output is stored in the cache. On every request to the page containing your control that can satisfy it from cache, the output will just simply be inserted - your first control will never be instantiated or run in that case, so the second one wont be either. Hope this helps,Mike VolodarskyProgram Manager...

customvalidator on a user control UC1 when the user clicks a button located on a different user control UC2 on the page
Hi, I am maintaining page where a user control is initialized  dynamically. The user control UC1 has a customvalidator using server side validation. When the postback is caused by a button inside the user control UC1 causes the postback, the customvalidator works fine and I get my validation results displayed. However, If I click a button located on a second user control UC2 on the main aspx page, the validation doesn t execute and I get no result validation error displayed. My question,how can I make the validation execute on the user control  UC1 when the postback is caused by...

User Controls not working if I put Validation controls in user control
Hi           I have validation controls(RequiredField Validator,RegularExpressionValidator)  in user controls.User controls are not posting back like the events are not wiring up.If I remove Validation controls in User controls page,user controls are working fine.Can anyone please help me out in getting it fixed. In the Validation controls, make sure you are setting the ControlToValidate property to the control that it links to.  If you're in .NET 2.0, it's also a good idea to put all the validation controls in the same Valid...

How to access a user control from another user control in a Wizard control
My page has a wizard control and each step is a user control.  I could find the value of a control in step1 user control in the wizard page but not in step2 user control.From wizard.aspx.vb – it worksDim m_title As DropDownList = CType(Me.PersonalBox1.FindControl("ddlTitle1"), DropDownList) Question:How can I find the value of a control(textbox or dropdownlist)in step1 user control from step2 user control? I want to find “PersonalBox1.FindControl("ddlTitle1")” from Step2 AddressBox1. I have tried several ways to access it but no luck. From AddressBox.ascx.vb –...

User Control controls not created when dynamically creating user control
  Have created a user control for displaying and editing a dynamically number of values. The user control contains two labels and an edit box. When dynamically loading the user control I get an error when the control itself tries to change setting on the fields within the user control.   Why doesnt this work? It works okay if I dont load it dynamically but create a user-control-instance in the aspx-page but thats no solution.   Thankyou!     Here is my code:   ucDataValue.ascx   <%@ Control Language="C#" AutoEventWireup="true&...

In a gridview, can I programatically control display of edit/delete buttons for selected row?
Hi there, I'm sure this is something fairly straight forward but I'm only just getting used to working with gridviews so please help me, I am displaying the select button on my gridview, and on the SelectedIndexChanged event I would like to add (or make visible) the edit and delete buttons just for the selected row - can I do this?  All help appreciated, JoH are you using template field with button / link button for edit or delete button  ?Thanx, [KaushaL] || BloG || Profile || Microsoft MVP"I would love to change the world, but they won’t give me ...

Help! Find row on which a button was pressed in the gridview control in .NET 2.0
How do you get the exact row (and column data from that row)  on which a button was pressed within a gridview. In .NET v1 using the datagrid control the followingconstruction was available:objArgs.Item.Cells(n).Textusing the datagrid DataGridCommandEventArgs parameter. choices in gridview are     Sub cmdBackTestSummary(ByVal objSender As Object, ByVal objArgs As GridViewCommandEventArgs) ' objArgs.commandname ' objargs.commandarguement ' objargs.commandsource     End Sub but no objArgs.Item.Cells(n) So how do you get the exact row (and c...

Web resources about - GridView, Edit Row, user control button - asp.net.presentation-controls

GridView SelectedItem Binding Gotcha in WinRT
Last night when I was doing some WinRT hacking I was trying to setup databinding to the selected item in the GridView control and I ran into ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Advanced Search Gridview Latest News Releases All ...

CNW Group
... Services Beyond the Wire About CNW my CNW CNW Access CNW Directories Financials Webcasts Events my CNW All News Releases Advanced Search Gridview ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Add a Filter Gridview Latest News Releases All Today ...

New CmdLets of Powershell 2.0
New CmdLets of Powershell 2.0

Price Drop: Softbox
... objects- create product reflection- changing the background with something more interestingUse it as:- softbox- honeycomb softbox- gridview ...

Profile.aspx: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Quick Answers - CodeProject
Questions - Free source code and tutorials for Software developers and Architects.; Updated: 25 Oct 2012

DotNetKicks.com - Upcoming stories
DotNetKicks.com is a community based news site edited by our members. It specialises in .NET development techniques, technologies and tools including ...

View Features - ExtendASP
Call 847-681-9827 or Request a Demo View Features Testimonials Pricing About Us Coming Soon ExtendASP exposes every feature, more than any other ...

Resources last updated: 1/20/2016 8:10:30 PM