views:

60

answers:

1

Hi Dears,
I have Created A Custom Control which is a DropDownList with specified Items. I designed AutoPostback and SelectedCategoryId as Properties and SelectedIndexChanged as Event for My Custom Control.
Here Is My ASCX file Behind Code:

private int _selectedCategoryId;

private bool _autoPostback = false;

public event EventHandler SelectedIndexChanged;

public void BindData()
{
    //Some Code...
}

protected void Page_Load(object sender, EventArgs e)
{
    BindData();
    DropDownList1.AutoPostBack = this._autoPostback;
}

public int SelectedCategoryId
{
    get
    {
        return int.Parse(this.DropDownList1.SelectedItem.Value);
    }
    set
    {
        this._selectedCategoryId = value;
    }
}

public string AutoPostback
{
    get
    {
        return this.DropDownList1.AutoPostBack.ToString();
    }
    set
    {
        this._autoPostback = Convert.ToBoolean(value);
    }
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (SelectedIndexChanged != null)
        SelectedIndexChanged(this, EventArgs.Empty);
}

I Want Used Update Panel to Update Textbox Fields According to dorp down list selected index.
this is my code in ASPX page:

<asp:Panel ID="PanelCategory" runat="server">
    <p>
        Select Product Category:&nbsp;
        <myCtrl:CategoryDDL ID="CategoryDDL1" AutoPostback="true" OnSelectedIndexChanged="CategoryIndexChanged"
            SelectedCategoryId="0" runat="server" />
    </p>
    <hr />
</asp:Panel>
<asp:UpdatePanel ID="UpdatePanelEdit" runat="server">
    <ContentTemplate>
        <%--Some TextBoxes and Other Controls--%>
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="CategoryDDL1" />
    </Triggers>
</asp:UpdatePanel>

But Always The Selected Index of CategoryDDL1 is 0(Like default). this means Only Zero Value will pass to the event to update textboxes Data. what is the wrong with my code? why the selected Index not Changing? Help?

+1  A: 

If your BindData() method is completely self-contained, move that from Page_Load to:

protected override void OnInit(EventArgs e)
{
   BindData();
}

This will keep your dropdown list in your control from being rebound on every page load, which I assume is the problem from the code that you've posted.

If, however, your BindData() method requires information from the parent page, change the page load to:

protected void Page_Load(object sender, EventArgs e)
{
    if(!this.Page.IsPostback) {
       BindData();
    }
    DropDownList1.AutoPostBack = this._autoPostback;
}

This will allow your dropdown to be bound only on the first page load, and subsequent loads should be able to access the properties correctly.

Also, be sure to check your ASPX page to make sure you're not binding the ASCX control on every page load. This can be resolved in the same way on the parent page.

Jim Schubert
thank you, but you know a ascx file Not inherited form Page Class. so we cant use Page.IsPostback(CMS_Controls_CategoryDropDown : System.Web.UI.UserControl). I have two Custom Dropdown List. First Dropdown fixed with your code(override OnInit) but 2nd drop down does not. and No change to the Index Occured for 2nd dropdown. what is your Idea? my 2nd dropdown located in update panel(and shows Parent Category) and its index will change when the 1st dropdown index changed.
mahdiahmadirad
mahdiahmadirad: a user control doesn't inherit from Page, but all controls have a property called `Page` which references the page in which the control is nested. So, in a user control, you can use `this.Page.IsPostback` to verify whether or not the parent page is currently in a postback.
Jim Schubert
Also, I'm not understanding the issue with the second dropdown. Can you edit your original post with the relevant code? My initial thought is that you need to set the `UpdateMode="Conditional"` on the update panel, and in your event, call `UpdatePanelEdit.Update()`. An example can be found here: http://www.ajaxtutorials.com/ajax-tutorials/updating-an-updatepanel-programmatically-in-c/
Jim Schubert