自定义控件中 DropDownList 的索引没有变化!

发布于 2024-09-05 15:51:58 字数 1872 浏览 4 评论 0原文

我创建了一个自定义控件,它是一个带有指定项目的 DropDownList。我将 AutoPostbackSelectedCategoryId 设计为属性,将 SelectedIndexChanged 设计为自定义控件的事件。
这是我的 ASCX 文件背后的代码:

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);
}

我想使用更新面板根据 dorp 下拉列表选择索引来更新文本框字段。
这是我在 ASPX 页面中的代码:

<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>

但 CategoryDDL1 的选定索引始终为 0(与默认值一样)。这意味着只有零值将传递给事件以更新文本框数据。我的代码有什么问题吗?为什么选定的索引没有改变?帮助?

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: 
        <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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2024-09-12 15:51:59

如果您的 BindData() 方法是完全独立的,请将其从 Page_Load 移至:

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

这将使控件中的下拉列表不会在每次页面加载时反弹,我认为这是问题所在您发布的代码。

但是,如果您的 BindData() 方法需要来自父页面的信息,请将页面加载更改为:

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

这将允许您的下拉列表仅在第一个页面加载时绑定,后续加载应该能够正确访问属性。

另外,请务必检查您的 ASPX 页面,以确保您没有在每次页面加载时绑定 ASCX 控件。可以在父页面用同样的方法解决。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文