This may seem like a simple one... but after searching the internet I can't seem to find a straight-foward solution to something that should be simple...
How do you save the selected value of a DropDownList.
I currently have the DropDownList getting its data(list items) from a LINQ connection and that part works great in my EditTemplate, but ..
I would like to send the selected item within the dropdown to my database when I click the update button. How can this be done??
Dim state As String = TryCast(sender, DropDownList).SelectedValue
TextBox2.Text = state
this works fine for a textbox placed outside of the ListView element but I can't expose anything within tagged items in vb code
how would I set ..for example
my bound field within the EditTemplate to be state:
field_3:<asp:TextBox ID="fl_3" runat="server" Text='<%# Eval("address_state") %>' />
TextBox txt = ListView1.Controls[0].FindControl("fl_3") as TextBox;
if (c != null)
{
txt.Text = ...;
}
H-SC
ASKER
here is what I have so far and does not seem to work.. Just not sure what I am doing wrong..
vb code:
Protected Sub d1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim txt As DropDownList = TryCast(ListView1.Controls(0).FindControl("d1"), DropDownList)
Dim txt2 As TextBox = TryCast(ListView1.Controls(0).FindControl("address_state"), TextBox)
If txt IsNot Nothing Then
txt2.Text = txt.Text
End If
End Sub
H-SC
ASKER
jasonduan,
on my last post, I do not get any errors,
however address_state textbox does not get populated with the selected text of the ddl("d1")
I have done breaks on the selectedindexchanged and it appears to be firing correctly but nothing is effected.
use "txt2.Text = txt.Selectedtext" instead of "txt2.Text = txt.Text"
H-SC
ASKER
yes, I have even tried that one and also
"txt2.Text = "Testing123" and still nothing
jasonduan
I'm afraid the postback causes the listview being repopulated and the textbox got overriden when the listview being repopulated. I would suggest you put a few breakpoints in the code to find out what exactly happened during postback.
"Object reference not set to an instance of an object."
this occured on the following
txt2.Text = "test"
Dim txt As DropDownList = TryCast(ListView1.Controls(0).FindControl("d1"), DropDownList)
Dim txt2 As TextBox = TryCast(ListView1.Controls(0).FindControl("textbox2"), TextBox)
If txt IsNot Nothing Then
txt2.Text = "test"
Else
>>>>>> txt2.Text = "test"
End If
End Sub
H-SC
ASKER
I think that I have it!
for some reason it likes the DirectCast object
Protected Sub d1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ddl As DropDownList = DirectCast(ListView1.EditItem.FindControl("d1"), DropDownList)
Dim txt2 As TextBox = DirectCast(ListView1.EditItem.FindControl("in_beds_conditionLabel"), TextBox)
txt2.Text = ddl.Text
End Sub
H-SC
ASKER
jasonduan,
this project is a room furniture condition checklist that has many elements of data, I think that by getting this one item working, that I can simply apply it throughout the rest of the checklist data. Many thanks for getting me on the right track!
can you post the related code?
d1.SelectedValue should give you value of the selected item, why it does not work?