如何使用Ajax更新面板根据国家(下拉列表)返回状态(下拉列表) [英] How To I Retrive The State(Dropdownlist) Based On The Country (Dropdownlist) Using Ajax Update Panel

查看:99
本文介绍了如何使用Ajax更新面板根据国家(下拉列表)返回状态(下拉列表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生/女士,

我想根据相应州想要检索的国家选择国家...但我已经掌握了存储在数据库表中的所有内容... < br $>


这是我的设计页面

Dear Sir/Madam,
I want to select country, based on the country the corresponding states wants to retrieve... But i have got everything whatever stored in the database table...

This is my design page

    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
   

    <div>
        
        
            <asp:UpdatePanel ID="CountryPanel" runat="server">
                <contenttemplate>
                    <tr>
                        <td>
                            <asp:Label ID="lbl_Country" runat="server" Text="Country" Width="150px">
                        </td>
                        <td>
                            <asp:DropDownList ID="DDL_Country" runat="server" Width="150px" 

                                AutoPostBack="true" AppendDataBoundItems="true" 

                                onselectedindexchanged="DDL_Country_SelectedIndexChanged">
                            
                        </td>
                    </tr>
                </contenttemplate>
                <triggers>
                    <asp:AsyncPostBackTrigger ControlID="DDL_Country" />
                </triggers>
            
            <br />

        <asp:UpdatePanel ID="StatePanel" runat="server">
        <contenttemplate>
        <tr>
            
        <td>
        <asp:Label ID="lbl_State" runat="server" Text="State" Width="150px">
        </td>
            <asp:DropDownList ID="DDL_State" runat="server" Width="150px" 

              AutoPostBack="true" AppendDataBoundItems="true" onselectedindexchanged="DDL_State_SelectedIndexChanged">
            
        <td>
        </td>
        </tr>
        </contenttemplate>
        <triggers>
        <asp:AsyncPostBackTrigger ControlID="DDL_State" />

        </triggers>
        
        <br />
        
    </div>
    </form>
</body>





这是我的代码隐藏文件



This is my code behind file

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Country();
    }
}

public void Country()
{
    con.Open();
    cmd = new SqlCommand("select CountryId,Country from Country", con);
    dr = cmd.ExecuteReader();
    DDL_Country.DataSource = dr;
    DDL_Country.Items.Clear();
    DDL_Country.Items.Add("-Select Country-");
    DDL_Country.DataTextField = "Country";
    DDL_Country.DataValueField = "CountryId";
    DDL_Country.DataBind();
    con.Close();

}

public void State()
{
    con.Open();
    cmd = new SqlCommand("select state_ID,State_Name from stat", con);
    dr = cmd.ExecuteReader();
    DDL_State.DataSource = dr;
    DDL_State.Items.Clear();
    DDL_State.Items.Add("-Select State-");
    DDL_State.DataTextField = "State_Name";
    DDL_State.DataValueField = "state_ID";
    DDL_State.DataBind();
    con.Close();

}

protected void DDL_Country_SelectedIndexChanged(object sender, EventArgs e)
{
    State();

}



请帮帮我...





考虑我有像印度,巴基斯坦,斯里兰卡这样的国家...

如果我选择印度意味着像Tamilnadu这样的印度州,卡纳塔克邦只想从数据库中检索。但在我的情况下,所有州(印度,巴基斯坦,斯里兰卡)从数据库中检索...



先谢谢...


Please help me...


Consider i have countries like India , Pakistan, Srilanka...
If i select India means India's states like Tamilnadu, Karnataka only want to retrieve from the database. But in my case all the states (India,Pakistan,Srilanka) retrieving from the db...

Thanks in Advance...

推荐答案

您好,

明智地选择州。相反,您已经从数据库中获取了所有州。所以,修改查询并在查询中给出 Where 条件。



了解有关级联下拉的详细信息

参考: Ajax中的级联下拉



谢谢
Hello ,
select the state country wise.Instead of that you have fetched all state from your database . so, modify the query and give a Where condition in your query.

for further details on cascading drop down
Refer: Cascading Dropdown in Ajax

thanks


public void State()

{

con.Open();

cmd = new SqlCommand(select state_ID,State_Name from stat where CountryId ='CountryId',con);

dr = cmd.ExecuteReader();

DDL_State.DataSource = dr;

DDL_State.Items.Clear();

DDL_State.Items.Add ( - 选择状态 - );

DDL_State.DataTextField =State_Name;

DDL_State.DataValueField =state_ID;

DDL_State .DataBind();

con.Close();



}
public void State()
{
con.Open();
cmd = new SqlCommand("select state_ID,State_Name from stat where CountryId='CountryId'", con);
dr = cmd.ExecuteReader();
DDL_State.DataSource = dr;
DDL_State.Items.Clear();
DDL_State.Items.Add("-Select State-");
DDL_State.DataTextField = "State_Name";
DDL_State.DataValueField = "state_ID";
DDL_State.DataBind();
con.Close();

}


<body>
    <form id="form1" runat="server">
    <asp:scriptmanager id="ScriptManager1" runat="server" xmlns:asp="#unknown">
    </asp:scriptmanager>
    <div>
        <asp:updatepanel id="CountryPanel" runat="server" xmlns:asp="#unknown">
            <contenttemplate>
                <tr>
                    <td>
                        <asp:label id="lbl_Country" runat="server" text="Country" width="150px"></asp:label>
                    </td>
                    <td>
                        <asp:dropdownlist id="DDL_Country" runat="server" width="150px" autopostback="true">
                            AppendDataBoundItems="true" OnSelectedIndexChanged="DDL_Country_SelectedIndexChanged">
                        </asp:dropdownlist>
                    </td>
                </tr>
            </contenttemplate>
            <triggers>
                <asp:asyncpostbacktrigger controlid="DDL_Country" />
            </triggers>
        </asp:updatepanel>
        <br />
        <asp:updatepanel id="StatePanel" runat="server" xmlns:asp="#unknown">
            <contenttemplate>
                <tr>
                    <td>
                        <asp:label id="lbl_State" runat="server" text="State" width="150px"></asp:label>
                    </td>
                    <asp:dropdownlist id="DDL_State" runat="server" width="150px" autopostback="true">
                        AppendDataBoundItems="true" OnSelectedIndexChanged="DDL_State_SelectedIndexChanged">
                    </asp:dropdownlist>
                    <td>
                    </td>
                </tr>
            </contenttemplate>
            <triggers>
                <asp:asyncpostbacktrigger controlid="DDL_State" />
            </triggers>
        </asp:updatepanel>
        <br />
        <asp:updatepanel id="CityPanel" runat="server" xmlns:asp="#unknown">
            <contenttemplate>
                <tr>
                    <td>
                        <asp:label id="lbl_City" runat="server" text="City" width="150px"></asp:label>
                    </td>
                    <td>
                        <asp:dropdownlist id="DDL_City" runat="server" width="150px" appenddatabounditems="true">
                            AutoPostBack="true">
                        </asp:dropdownlist>
                    </td>
                </tr>
            </contenttemplate>
            <triggers>
                <asp:asyncpostbacktrigger controlid="DDL_City" />
            </triggers>
        </asp:updatepanel>
    </div>
    </form>
</body>


这篇关于如何使用Ajax更新面板根据国家(下拉列表)返回状态(下拉列表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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