在gridview asp.net c#中第二次尝试刷新DropDownList

时间:2015-07-11 04:21:56

标签: c# asp.net gridview

通过选择下拉项目添加新记录时遇到问题。基本上它是一个gridview,我在其中选择AddressType,Country然后选择City然后选择CityArea,依此类推。在第一次尝试时,它工作正常,但在第二次尝试时,当我选择国家时,它会重置所选项目。

http://i.stack.imgur.com/GoTtU.jpg

如上图所示,第二次尝试我将选择国家。一旦我选择了国家它刷新。请看下图:

http://i.stack.imgur.com/ObeT5.jpg

请帮我找到解决方案。 感谢。

这是我的代码:

<asp:TemplateField HeaderText="Country" HeaderStyle-Width="14%">
                        <ItemTemplate>
                            <asp:Label ID="lblCountry" Text='<%# DataBinder.Eval(Container, "DataItem.Country.Description") %>'
                                runat="server">   </asp:Label>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:UpdatePanel runat="server" ID="UPCountry">
                            <ContentTemplate>
                                <asp:DropDownList ID="ddlCountryNew" DataSourceID="odsCountry" runat="server" OnSelectedIndexChanged="ddlCountryNew_SelectedIndexChanged"
                                    AutoPostBack="true" DataTextField="Description" DataValueField="Id" CssClass="myWidth-7" />
                            </ContentTemplate>
                            <Triggers>
                                <asp:AsyncPostBackTrigger ControlID="ddlCountryNew" />
                            </Triggers>
                        </asp:UpdatePanel>
                        </FooterTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlCountry" DataSourceID="odsCountry" runat="server" SelectedValue='<%# DataBinder.Eval(Container, "DataItem.CountryId") %>'
                                DataTextField="Description" DataValueField="Id" CssClass="myWidth-6" ValidationGroup="EditAddressGroup" />
                        </EditItemTemplate>
                    </asp:TemplateField>

C#:

protected void ddlCountryNew_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlCountryNew = (DropDownList)sender;
        hdnCountryId.Value = ddlCountryNew.SelectedItem.Value;
    }

1 个答案:

答案 0 :(得分:0)

如果您希望下拉列表触发Ajax调用而不刷新页面

设置EventName="SelectedIndexChanged",如下所示

<Triggers> 
<asp:AsyncPostBackTrigger ControlID="ddlCountryNew" EventName="SelectedIndexChanged" /> 
</Triggers> 

仔细查看Update Panel MSDN document with example