drop downlist selected value
ddlType.DataSource = ObjComplaintReportFormBLL.ComplaintType();
ddlType.DataTextField = "ComplaintType_Name";
ddlType.DataValueField = "complainttype_id";
ddlType.DataBind();
ddlType.Items.Insert(0, "All");
ObjComplaintReportFormBLL.ComplaintType() returns the Co开发者_如何学PythonmplaintType_Name, complainttype_id
All is the default value for the drop down list Now how should I set the value of this list item "All" to 0(int)
I could do
ddlType.Items[0].value = "0".
But this is a string
Thanks Sun
Replace
ddlType.Items.Insert(0, "All");
with
ddlType.Items.Add(new ListItem(0,"All"));
By using Insert
, you're putting the text "All" as the first option in the dropdown with a value of "" (empty string). To get "0" to be your value for your "All" item, provide a ListItem
:
ddlType.Items.Add(new ListItem("0", "All"));
or, perhaps closer to what you want to do, this will insert your "All" item at the start of the list:
ddlType.Items.Insert(0, new ListItem("0", "All"));
then:
ddlType.SelectedValue = "0";
You can pass the listItem object to the ddlType.Items.Insert
method instead of passing a string value e.g.
ListItem liItem = new ListItem("All","0");
ddlType.Items.Insert(0,liItem);
精彩评论