My problem is I want to add in a value to a LABEL based on selected value in the DropDownList !!


Example:

tbl_abc

ID Sequence Name
-----------------------------------------------
1 2 Michael
2 3 Jenny
3 4 Max
4 4 Max
5 2 Michael
6 4 Max
7 3 Jenny
8 5 Alice
-----------------------------------------------

ID-(asp:label)
Sequence-(asp:label)
Name-(asp:DropDownList)


-If I choose Max from the DropDownList then the Sequence_label will automatically generate the 4.
-If I choose Michael from the DropDownLst then the Sequence_label will automatically generate the 2.


Can anyone help me to solve this problem??

Thanks in advance !!

Dani AI

Generated

A concise diagnosis and two safe fixes that fit the existing thread (ties to and ).

The core choices are: store the needed Sequence value in the DropDownList so no extra DB round-trip is required, or keep the DropDownList bound to an ID and fetch Sequence on selection. Both approaches were suggested in the replies: pointed to using the selected value, and suggested querying the DB on SelectedIndexChanged. The original code posted had a few concrete problems — C# syntax errors (use = for assignment, not ==), and the DropDownList lives inside a template so the label must be located through the control's NamingContainer.

Approach A — bind Sequence as the DropDownList value (fast, no query):

protected void DropDownList_AreaBucket_SelectedIndexChanged(object sender, EventArgs e)
{
    var ddl = (DropDownList)sender;
    var container = ddl.NamingContainer;
    var seqLabel = (Label)container.FindControl("Sequence_label");
    if (seqLabel != null)
    {
        // assumes DataValueField was set to the SEQUENCE column when binding
        seqLabel.Text = ddl.SelectedValue;
    }
}

Approach B — keep value as ID and fetch Sequence server-side (parameterized, safe):

private string GetSequenceFromDb(string id)
{
    using (var cn = new SqlConnection(connectionString))
    using (var cmd = new SqlCommand("SELECT TOP 1 SEQUENCE FROM tbl_abc WHERE ID = @id", cn))
    {
        cmd.Parameters.AddWithValue("@id", id);
        cn.Open();
        var obj = cmd.ExecuteScalar();
        return obj == null ? string.Empty : obj.ToString();
    }
}

Call the above from the same SelectedIndexChanged handler when DataValueField is ID.

Quick checklist (common pitfalls): AutoPostBack must be true or wire the event in code-behind; OnSelectedIndexChanged name in markup must match the handler; cast sender to DropDownList and use NamingContainer.FindControl to reach the Sequence label; avoid string concatenation in SQL — use parameters and using blocks; consider an UpdatePanel or client-side attribute storage to avoid full postbacks.

Recommended Answers

All 17 Replies

Hi
You can do this on change event of Ur DropdownList.

Like
on DD1.selectedindex_chnged()
if DD1.selectedvalue == 1 then
{
lbl1.text = DD1.selectedValue()
}

Try this and if need more help then i am here.

Best Luck.

Hi sbv,

If I follow the your statement then I only can get the drop down list value and add into the label but what I want is not this.

I want is like this that I mentioned above:
-If I choose Max from the DropDownList then the Sequence_label will automatically generate the 4.
-If I choose Michael from the DropDownLst then the Sequence_label will automatically generate the 2.


Thanks ya

hi hwa,
ok. But the meathod i told is right.
You can assign any value to label using this.
Is all values are in Db table? If yes then fetch it as per Records selected in DropDown.
As... on Selected index change.
fire a query something like...

"select Squesnce from tbl_abc where ID = " + DD1.Selectedvalue

and assign its value to label.

Now i have given right solution or still not. Still you need something different ?
If so then i am sorry. I am not clear with your requirement.

Have a good time. Bye.

you can do it by exactly what he said. An example of this would be:

labelname.Text = dropdownlistname.SelectedItem.Value

you can do it by exactly what he said. An example of this would be:

labelname.Text = dropdownlistname.SelectedItem.Value

Hi,

-May I know that this code only can retrieve the selected value in DropDown to the Label right???

-Actually is my misunderstanding or yours not clear about my requirement??

-the data inside tbl_abc is fix !!

-sorry, can yours give more clearly about that

Thanks ya

Hi sbv,

Can you explain more clearly about it because I still confuse about it.

-I retrieve the tbl_abc from sqlDatabase
-I using SqlDataSource(is it needed??)
-I using <asp:Label> for the Sequence (this label will show the value based on the Selected value in the DropDown)
-I using <asp:DropDownList> for the Name (this will choose by the user)

All I do is in the <InsertItemTemplate>


Thanks for give me a hand !!

My problem is I want to add in a value to a LABEL based on selected value in the DropDownList !!


Example:

tbl_abc

ID Sequence Name
-----------------------------------------------
1 2 Michael
2 3 Jenny
3 4 Max
4 4 Max
5 2 Michael
6 4 Max
7 3 Jenny
8 5 Alice
-----------------------------------------------

ID-(asp:label)
Sequence-(asp:label)
Name-(asp:DropDownList)


-If I choose Max from the DropDownList then the Sequence_label will automatically generate the 4.
-If I choose Michael from the DropDownLst then the Sequence_label will automatically generate the 2.


Can anyone help me to solve this problem??

Thanks in advance !!

Hi,

Do you want to assign ID and Sequence values both to the label depending upon the selection of DropDownLst?

As you said here -
ID-(asp:label)
Sequence-(asp:label)
Name-(asp:DropDownList)

I consider yes.

As i told you in my last post. On DropDownList's selected index change event
you need to do 2 things.
1) fetch ID and Sequence from db depending upon the DropdownLst value
2) Then assign it to your labels.

Thanks all

I had solve my problem. Yours are right.
Thats all is my mistake


Thanks very very much

hi

:D

::D

Hi,

Do you want to assign ID and Sequence values both to the label depending upon the selection of DropDownLst?

I consider yes.

As i told you in my last post. On DropDownList's selected index change event
you need to do 2 things.
1) fetch ID and Sequence from db depending upon the DropdownLst value
2) Then assign it to your labels.

Hi sbv,

I had problem again, can you show me the code in the step 1??

Thanks

Hi
ok. here it is....


on selected index change of ur dd. write a query llike

SQL="select * from tble where id(pk)=" & dd.selectedvalue
rd = clsComm.GetReader(sql) 'excute query
If rd.Read = True Then
If Not IsDBNull(rd("Sequence")) Then lblSequence.Text = "R-" & rd("Sequence")
'same for other
If rd.IsClosed = False Then rd.Close()

Hi
ok. here it is....


on selected index change of ur dd. write a query llike

SQL="select * from tble where id(pk)=" & dd.selectedvalue
rd = clsComm.GetReader(sql) 'excute query
If rd.Read = True Then
If Not IsDBNull(rd("Sequence")) Then lblSequence.Text = "R-" & rd("Sequence")
'same for other
If rd.IsClosed = False Then rd.Close()

Hi sbv,

this seem not like c# code, can you show in c# !!
Thanks

hi
ur right its a vb.net code
here is c# code.

SQL == "select * from tble where id(pk)=" + dd.selectedvalue
//excute query 
rd == clsComm.GetReader(sql)
    if (rd.Read == true)
 { 
        if (!Information.IsDBNull(rd("Sequence"))) 
            lblSequence.Text = rd("Sequence"); 
        //same for other 
    } 
    if (rd.IsClosed == false) 
        rd.Close();

i have used code converter here. please verify code.

if u need then here is the link of online code converter

best luck

hi
ur right its a vb.net code
here is c# code.
SQL == "select * from tble where id(pk)=" + dd.selectedvalue
//excute query
rd == clsComm.GetReader(sql)
if (rd.Read == true)
{
if (!Information.IsDBNull(rd("Sequence")))
lblSequence.Text = rd("Sequence");
//same for other
}
if (rd.IsClosed == false)
rd.Close();

Hi sbv,

Actually my code is something as below:

<asp:TemplateField HeaderText="AREA_BUCKET" SortExpression="AREA_BUCKET">
                    <EditItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("AREA_BUCKET") %>'></asp:Label>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <asp:DropDownList ID="DropDownList_AreaBucket" runat="server" DataSourceID="AreaBucket_SqlDataSource"
                            DataTextField="AREA_BUCKET" DataValueField="AREA_BUCKET" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
                             SelectedValue='<%# Bind("AREA_BUCKET") %>' Width="140px" AutoPostBack="true">
                        </asp:DropDownList>&nbsp;&nbsp;
                            
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("AREA_BUCKET") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>


<asp:TemplateField HeaderText="SEQUENCE" SortExpression="SEQUENCE">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("SEQUENCE") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        &nbsp;&nbsp;
                        <asp:Label ID="Sequence_label" runat="server" Text='<%# Bind("SEQUENCE") %>'></asp:Label>
                        
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("SEQUENCE") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

My code behind is here:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
       SQL == "select * from tble where id(pk)=" + dd.selectedvalue
      //excute query 
       rd == clsComm.GetReader(sql)
        if (rd.Read == true)
        { 
        if (!Information.IsDBNull(rd("Sequence"))) 
            lblSequence.Text = rd("Sequence"); 
        //same for other 
        } 
          if (rd.IsClosed == false) 
           rd.Close();
}

the code behind doesnt work at all
May you help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.