Reputation: 13
ok, when i want to get the SelectedValue of the dropdownlist when i click a button, but the SelectedValue always return "" (nothing), please help me thanks!
here is the source
<tr>
<th style="float:left">
<asp:DropDownList ID="ddlcategory" runat="server" ></asp:DropDownList>
<asp:TextBox ID="txtsearch" runat="server"></asp:TextBox>
<asp:ImageButton ID="btnsearch" runat="server" Height="20px" ImageUrl="~/img/search.png" OnClick="btnsearch_Click" Width="20px" />
<asp:LinkButton ID="lbtnsearch" runat="server" PostBackUrl="~/Search.aspx">Advanced Search</asp:LinkButton>
</th>
</tr>
and here is the code behind
public partial class _default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlDataAdapter adapter = new SqlDataAdapter();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataList();
BindDropDown();
}
}
private void BindDataList()
{
string cmd = "SELECT top 3 product_id, product_name, brand, imgurl FROM product ORDER BY NEWID()";
adapter.SelectCommand = new SqlCommand(cmd, con);
DataTable dt = new DataTable();
adapter.Fill(dt);
DataList1.DataSource = dt;
DataList1.DataBind();
}
private void BindDropDown()
{
string cmd = "select category_id, category_name from category";
adapter.SelectCommand = new SqlCommand(cmd, con);
DataTable dt = new DataTable();
adapter.Fill(dt);
ddlcategory.DataTextField = "category_name";
ddlcategory.DataValueField = "category_id";
ddlcategory.DataSource = dt;
ddlcategory.DataBind();
}
protected void btnsearch_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("~/Product/Product.aspx?category_id=" + ddlcategory.SelectedValue.ToString() + "&search=" + txtsearch.Text);
}
}
and this is the result when i click btnsearch "http://localhost:1345/Product/Product.aspx?category_id=&search=something" as you can see that that ddlcategory.SelectedValue.ToString() return "", what's wrong?
Upvotes: 0
Views: 71
Reputation: 13
Damn, i spent hours, and finally solve this problem. The problem is that the EnableViewState is set to false in the master page (the parent Control). and all you need is set it back to true. even if the DropDownList (child control) is enabled viewstate, it also need to determine on the parent control. i.e. the EnableViewState in the parent control is set to false, the child control will also be false.
Upvotes: 1