i have created a dropdown list that is populated from a sqlserver datasource.
i have a separate sql command which uses the dropdownlist.selected value as a parameter and returns the command value to a label.
it mostly works fine but with one problem. when the page loads, the label value is null, because it doesn't seem to treat the dropdownlist default value as a selected value. when i use the dropdownlist to select a value, this is fine and the label begins to display the expected value.
is there anyway I can make the default value when the page is loaded act like a manually selected value, so the label control never displays a null value?
![]() |
0 |
![]() |
Can you post you code...
If your requirement is to Insert your own Item at the top and mark it as selectyed then here is the code
ddlType.DataSource = objclsReservation.getTitles(); ddlType.DataTextField = "str_Title"; ddlType.DataValueField = "pk_TitleId"; ddlType.DataBind(); ddlType.Items.Insert(0, new ListItem("Please Select", "0"));If you want to select a value alrady exist in your records then use this piece of code
string strMemberTitle = ObjMember.Title; // Write your value to be selected for (int i = 0; i < ddlMembership.Items.Count; i++) { if (ddlMembership.Items[i].Text.Equals(strMemberTitle)) { ddlMembership.SelectedItem = strMemberTitle; } }
Please MARK post as ANSWERED, if you find answer helpful
http://www.linkedin.com/in/shantanushukla
![]() |
0 |
![]() |
Hello,
Use this
ASPX page
<asp:ListBox ID="listbox1" runat="Server" AutoPostBack="true" Width="134px"></asp:ListBox>
<asp:Label ID="lbltest" runat="server"></asp:Label>
ASPX.CS code behind page c# language
protected void Page_Load(object sender, EventArgs e)
{
LoadDropDownList();
}
//To bind the data to drop down list
private void LoadDropDownList()
{
DataSet ds1 = new DataSet();
ds1 = GetTypeListItem();
listbox1.DataSource = ds1;
listbox1.DataValueField = "Value";
listbox1.DataTextField = "Text";
if (ds1.Tables[0].Rows.Count >= 0)
{
listbox1.SelectedIndex = 0; // You can change this 1,2,3...list item selection
}
listbox1.DataBind();lbltest.Text = DropDownList1.SelectedValue;
I hope this will hep you.
}
//To Get the list data from db using sproc
public DataSet GetTypeListItem()
{
SqlCommand sqlCmd = Utility.GetDBConnection("Sproc Name");
sqlCmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
Thanks & Regards
Sandeep Reddy Pasham
GGK Technologies, Hyd
www.ggktech.com
http://aspdotnetdevs.blogspot.com
~ Please remember to click Mark as Answer on this post if it helped you ~
![]() |
0 |
![]() |
the dropdownlist is created this way:<
asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource3" DataTextField="Expr3" DataValueField="Expr3"> </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:AMBANTCONNECTIONSTRING %>" SelectCommand="SELECT DISTINCT MONTH(EntryDate) AS Expr1, YEAR(EntryDate) AS Expr2, CAST(MONTH(EntryDate) AS varchar) + '-' + CAST(YEAR(EntryDate) AS varchar) AS Expr3 FROM entrydata WHERE (YEAR(EntryDate) IS NOT NULL) ORDER BY Expr2"> </asp:SqlDataSource>and it displays Expr3 which shows values like "12-2008", "1-2009" etc
my c# / sql code for using the dropdownlist value as a parameter in another query is this:
string
strSQLCommand7 = "select SUM(ElapsedTime) from entrydata where CAST(MONTH(EntryDate) AS varchar) + '-' + CAST(YEAR(EntryDate) AS varchar) = @mnthyrs;";SqlCommand command7 = new SqlCommand(strSQLCommand7, conn);command7.Parameters.AddWithValue((
string) "@mnthyrs", DropDownList1.SelectedValue); string returnvalue7 = (string)command7.ExecuteScalar().ToString();and i have a label called monthtothrs which i return the value to in this way
mnthtothrs.Text = returnvalue7;
when the page loads, thedropdownlist shows "12-2008" but the label doesn't show anything. if i use the dropdownlist to select something, the label shows me the right data. what i need is that when the page loads, it treats the value displayed in the dropdownlist as "selected" so it will pass a value to the label.i hope i explained it correctly - i'm not exactly an expert!
![]() |
0 |
![]() |
Hi, call your code to fill your label additional in the SqlDataSource3_selected event protected void odsDetails_Selected(object sender, ObjectDataSourceStatusEventArgs e) { DataView dv = (DataView)e.ReturnValue; String selectedValue = dv[0][columnname].ToString(); // Get the Value from the first row CallYourFunction (selectedValue); }
-------------------------------------------------
Jürgen
![]() |
0 |
![]() |
what do i put in place of "CallYourFunction"? i don't have any functions
![]() |
0 |
![]() |
Hi
try this example to validate bindable dropdownlist to make user must select item from it using RequiredField Validator:
Select Area :
<asp:DropDownList ID="ddl_Area" runat="server" AppendDataBoundItems="True" DataSourceID="SqlDataSource_Areas" DataTextField="AreaName" DataValueField="AreaID" Width="154px" > <asp:ListItem Value="0" >Select Area ...</asp:ListItem> </asp:DropDownList> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="ddl_Area" ErrorMessage="Select Area." InitialValue="0" SetFocusOnError="True" >*</asp:RequiredFieldValidator>In .NET 2.0, this can be done declaratively using the AppendDataBoundItems property. This will append all data-bound ListItems to the DropDownList, leaving those you add manually as the first selections.
Good Luck
![]() |
0 |
![]() |
Hi,
i mean that you extract your code to get the value for your textbox.
Put this in a single function with one parameter for the selectedvalue as string.
Then call this function in from selectedindexchange and the _selected statement
-------------------------------------------------
Jürgen
![]() |
0 |
![]() |