回发后重置下拉列表选择的值

时间:2016-05-22 07:18:35

标签: c# sql asp.net drop-down-menu

我是asp.net的新手,我有一些代码来修复dropdownlist。用户点击搜索按钮后,所选的值将被重置。你能帮我解决这个问题吗?

这是设计页面

<fieldset>
<legend>Search</legend>

            <table class="tblsearch">
                <tr>
                    <td style="width:80px">Category:</td>
                    <td>
                        <asp:DropDownList ID="DDlistMainCat" runat="server" DataSourceID="sdmaincat" DataTextField="MainCategory" DataValueField="MainCategoryId" AutoPostBack="True">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="sdmaincat" runat="server" ConnectionString="<%$ ConnectionStrings:CString %>" SelectCommand="SELECT * FROM [Tbl_MainCategory]"></asp:SqlDataSource>
                    </td>
                </tr>
              <tr>
                    <td>Sevices:</td>
                    <td>
                        <asp:DropDownList ID="DDlistCat" runat="server" DataSourceID="sdcat" DataTextField="Category" DataValueField="CategoryId">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="sdcat" runat="server" ConnectionString="<%$ ConnectionStrings:CString %>" SelectCommand="SELECT [CategoryId], [Category] FROM [Tbl_Category] WHERE ([MainCategoryRef] = @MainCategoryRef)">
                            <SelectParameters>
                                <asp:ControlParameter ControlID="DDlistMainCat" Name="MainCategoryRef" PropertyName="SelectedValue" Type="Int32" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                    </td>
                </tr>
            </table>
   </fieldset> 
   <fieldset>

   <table class="tblsearch">
                <tr>
                    <td style="width:80px">Nationality:</td>
                    <td>
                        <asp:DropDownList ID="DDlistNat" runat="server" DataSourceID="sdnat" DataTextField="Nationality" DataValueField="NationalityId" EnableViewState="true">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="sdnat" runat="server" ConnectionString="<%$ ConnectionStrings:CString %>" SelectCommand="SELECT * FROM [Tbl_Nationality]"></asp:SqlDataSource>
                     </td>
                </tr>



          <tr>
                    <td>Age:</td>
                    <td>
                        <asp:DropDownList ID="DDlistAge" runat="server" DataSourceID="sdage" DataTextField="Age" DataValueField="AgeV">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="sdage" runat="server" ConnectionString="<%$ ConnectionStrings:CString %>" SelectCommand="SELECT [AgeV], [Age] FROM [Tbl_Age]"></asp:SqlDataSource>
                    </td>
                </tr>
                 <tr>
                    <td>Experience:</td>
                    <td>
                        <asp:DropDownList ID="DDlistExp" runat="server" DataSourceID="sdexp" DataTextField="Exp" DataValueField="ExpV">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="sdexp" runat="server" ConnectionString="<%$ ConnectionStrings:CString %>" SelectCommand="SELECT [ExpV], [Exp] FROM [Tbl_Exp]" ></asp:SqlDataSource>
                     </td>
                </tr>
                  <tr>
                    <td>Gender:</td>
                    <td>
                        <asp:DropDownList ID="DDlistGen" runat="server">
                            <asp:ListItem>Male</asp:ListItem>
                            <asp:ListItem>Female</asp:ListItem>
                        </asp:DropDownList>
                      </td>
                </tr>
                  <tr>
                    <td></td>
                    <td>
                        <asp:Button ID="BtnSearch" class="btn" runat="server" Text="Search" OnClick="BtnSearch_Click" />
                       </td>
                </tr>
            </table>



</fieldset>

这就是行动

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["ClientId"] == null)
        {
            MyAccBox.Visible = false;
        }
        else
        {
            MyAccBox.Visible = true;
            lblfullname.Text = Session["Fullname"].ToString();
        }
    }

    protected void BtnSearch_Click(object sender, EventArgs e)
    {
        Response.Redirect("View.aspx?cat=" + DDlistCat.SelectedValue + "&nat=" + DDlistNat.SelectedValue + "&age=" + DDlistAge.SelectedValue + "&exp=" + DDlistExp.SelectedValue + "&gen=" + DDlistGen.SelectedValue + "");
    }
}

1 个答案:

答案 0 :(得分:0)

如果您从!IsPostBack中的代码中绑定下拉列表,则不会发生这种情况。

if(!IsPostBack)   
{
DDlistExp.DataSource = DataSet.Tables[0];    
DDlistExp.DataValueField = "ExpV";
DDlistExp.DataTextField = "Exp";
DDlistExp.DataBind();    
}