如何动态绑定用户控件(.ascx)页面中的两个相对下拉列表 [英] how to bind two relative drop down in user control (.ascx) page dynamically

查看:84
本文介绍了如何动态绑定用户控件(.ascx)页面中的两个相对下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以这样一种方式绑定我的两个下拉列表,在一个下拉列表的选定索引上填充其相对值。我需要在用户控制页面上进行此活动,因为我的表单仅在.ascx页面上,但问题是当选择索引调用它再次重新加载页面并且我的控件设置为其初始值而第二次下拉时未填充其值..什么要做.. ???



请帮帮我,告诉我如何控制回发这里



提前谢谢

解决方案

请考虑使用updatepanel包含使用它们的页面上的两个控件,并在同一页面上处理selectindexchange事件(现在不会回发再次)。





更新:



这是一个基本的例子当选择后者时,第二次下拉有第一个下拉值。



页面:



 <%@    页 < span class =code-attribute>  语言  =  C#    AutoEventWireup   =   true    CodeBehind   =  WebForm1.aspx.cs   继承  =  dummyweb.WebForm1 < span class =code-attribute>  %>  

< %@ 注册 < span class =code-attribute> Src = 〜/ WebUserControl1.ascx TagPrefix = uc1 TagName = WebUserControl1 %>
<%@ 注册 Src = 〜/ WebUserControl2.ascx TagPrefix = uc1 TagName = WebUserControl2 %>



< !DOCTYPE html >

< html xmlns = http: //www.w3.org/1999/xhtml\">
< head runat = 服务器 >
< title > < / title >
< / head >
< body >
< form id = form1 runat = server >
< asp:ScriptManager runat = 服务器 > < / asp:ScriptManager >
< div >
< asp:UpdatePanel ID = UpdatePanel1 runat = server >
< ContentTemplate >

< uc1:WebUserControl1 runat = server ID = WebUserControl1 OnSelectIndexChanged = WebUserControl1_SelectIndexChanged / >
< uc1:WebUserControl2 runat = server ID = < span class =code-keyword> WebUserControl2 / >

< / ContentTemplate >
< / asp:UpdatePanel >
< / div >
< / form >
< / body >
< / html >







PAGE.cs



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;

命名空间 dummyweb
{
public partial class WebForm1:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{

}

受保护 void WebUserControl1_SelectIndexChanged( object sender,EventArgs e)
{
DropDownList ddl =(DropDownList)sender;
WebUserControl2.DropDown.SelectedValue = ddl.SelectedValue;
}
}
}









UserControl1





 <%@    控制   语言  =  C#    AutoEventWireup   =  true    CodeBehind   =  WebUserControl1.ascx.cs   继承  =  dummyweb.WebUserControl1   %>  

< asp :DropDownList ID = DropDownList1 runat = server AutoPostBack = true OnSelectedIndexChanged = DropDownList1_SelectedIndexChanged >
< asp:ListItem 文字 = item1 = 1 > < / asp :ListItem >
< asp:ListItem 文字 = item2 < span class =code-keyword> = 2 > < / asp:ListItem >
< asp:ListItem 文字 = item3 = 3 > < / asp:ListItem >
< asp:ListItem 文本 = item4 = 4 &g t; < / asp:ListItem >
< / asp:DropDownList >







UserControl1.cs



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;

命名空间 dummyweb
{
public partial class WebUserControl1:System.Web.UI.UserControl
{
protected void Page_Load( object sender,EventArgs e)
{

}

public event EventHandler SelectIndexChanged;

protected void DropDownList1_SelectedIndexChanged( object sender,EventArgs e)
{
SelectIndexChanged(sender,e);
}

}
}







UserControl2



 <%@    控制   语言  =  C#    AutoEventWireup   =  true    CodeBehind   =  WebUserControl2.ascx.cs   继承  =  dummyweb.WebUserControl2   %>  
< asp:DropDownList ID = DropDownList1 runat = server >
< ; asp:ListItem 文字 = item1 = 1 > < / asp:ListItem >
< asp :ListItem 文本 = item2 = 2 > < / asp:ListItem >
< asp:ListItem 文本 = item3 = 3 > < / asp:ListItem >
< asp:ListItem 文本 = item4 = 4 > < / asp:ListItem >
< / asp:DropDownList >







UserControl2.cs



<前lang =cs> 使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;

命名空间 dummyweb
{
public partial class WebUserControl2:System.Web.UI.UserControl
{
public DropDownList DropDown { get { return DropDownList1; }
受保护 void Page_Load( object sender,EventArgs e)
{

}
}
}


i am trying to bind my two drop down in such a way that on selected index of one dropdown second fill with its relative values. I need this activity on user control page because my form is on .ascx page only but the problem is when selected index calls it reload the page again and my controls set on its initial values and second drop down is not filling its value.. what to do..???

please help me and tell me how to control postback here

thanks in advance

解决方案

Please, consider using updatepanel envolving both controls on the page that uses them, and handling the selectindexchange event on that same page (which will now not postback anymore).


Update:

this is a basic example which makes the second drop down has the first dropdown's value when the latter is selected.

Page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="dummyweb.WebForm1" %>

<%@ Register Src="~/WebUserControl1.ascx" TagPrefix="uc1" TagName="WebUserControl1" %>
<%@ Register Src="~/WebUserControl2.ascx" TagPrefix="uc1" TagName="WebUserControl2" %>



<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager runat="server"></asp:ScriptManager>
        <div>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>

                    <uc1:WebUserControl1 runat="server" ID="WebUserControl1" OnSelectIndexChanged="WebUserControl1_SelectIndexChanged" />
                    <uc1:WebUserControl2 runat="server" ID="WebUserControl2" />

                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>




PAGE.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace dummyweb
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void WebUserControl1_SelectIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            WebUserControl2.DropDown.SelectedValue = ddl.SelectedValue;
        }
    }
}





UserControl1


<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="dummyweb.WebUserControl1" %>

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
    <asp:ListItem Text="item1" Value="1"></asp:ListItem>
    <asp:ListItem Text="item2" Value="2"></asp:ListItem>
    <asp:ListItem Text="item3" Value="3"></asp:ListItem>
    <asp:ListItem Text="item4" Value="4"></asp:ListItem>
</asp:DropDownList>




UserControl1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace dummyweb
{
    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public event EventHandler SelectIndexChanged;

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectIndexChanged(sender, e);
        }

    }
}




UserControl2

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="dummyweb.WebUserControl2" %>
<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Text="item1" Value="1"></asp:ListItem>
    <asp:ListItem Text="item2" Value="2"></asp:ListItem>
    <asp:ListItem Text="item3" Value="3"></asp:ListItem>
    <asp:ListItem Text="item4" Value="4"></asp:ListItem>
</asp:DropDownList>




UserControl2.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace dummyweb
{
    public partial class WebUserControl2 : System.Web.UI.UserControl
    {
        public DropDownList DropDown { get { return DropDownList1; } }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}


这篇关于如何动态绑定用户控件(.ascx)页面中的两个相对下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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