Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Label Text value to Dropdownlist selected value

Status
Not open for further replies.

bubberz

Programmer
Dec 23, 2004
117
US
I have a dropdownlist in a datagrid that get's shown to the user once the "Edit" button from the Datagrid is clicked. I have the dropdown set @ PostBack=True

How do I set a label on the page to equal the dropdownlist's selected value on postback.

Is it in the HTML text attribute of the label?
Something like below for the label's text? I'm getting close, but not yet there!
..the datagrid id is DataGrid1, and the dropdown is ddlP3

text='<%# DataBinder.Eval(Container.DataGrid1.Findcontrol("ddlP3.SelectedValue.ToString()") %>'
 
I think you need to remove the DataBider part. Try This:
textbox1.text= Container.DataGrid1.Findcontrol(ddlP3.SelectedValue.ToString()) %>'
 
Thanks jbenson001!

I tried that in the HTML section of the aspx page and got:
'DataGrid1' is not a member of 'System.Web.UI.Control'
 
I put this in the label load handler:
lblHelp.Text = <%# Container.DataGrid1.Findcontrol(ddlP3.SelectedValue.ToString())%>

I must have some syntax error...I get the blue squiggly line of "Expression Expected
 
lblHelp.Text = DataGrid1.FindcontrolddlP3.SelectedValue.ToString()
 
jbenson001,

Isn't FindControl a function, and should be like:

FindControl(ddlP3.SelectedValue.ToString())

I keep getting errors saying the value of the control can't be converted to a string
 
FindControl is a method that exists on many different controls. See the VS Help for more information.

Dim ddl As DropDownList
ddl = dg1.FindControl("ddl")
Label1.Text = ddl.SelectedItem.Text
 
Actually for what you are trying to do, you need to do a couple of things.

First you need to set up a handler for your dropdownlist in the HTML of the datagrid.
Code:
<EditItemTemplate>
   <asp:DropDownList id="ddl1" runat="server"
AutoPostBack="True" [b]OnSelectedIndexChanged="ddlSelectionChanged">[/b]
</EditItemTemplate>

Then you need to code the event handler in your code behind.
Code:
Protected Sub ddlSelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ddl As DropDownList
        ddl = CType(sender, DropDownList)
        Label1.Text = ddl.SelectedItem.Text

That should do what you are looking for. It worked in my test page.
 
Cool....thanks jbenson001!
I'll try that tomorrow and let you know!

Thanks again for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top