I've been looking for a solution to a problem when using one dropdownlist within a gridview to populate another dropdownlist.
The issue is that the databinding is broken on the second dropdownlist once you do this, so you can't use SelectedValue='<%# Bind("....") %>' or else you'll get an error when you go to do an update to your database. ("Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control. ").
Here's the relevant code snippets:
function jobTitleToHiddenField(strListClientID, strHiddenFieldClientID)
var strJobTitle = document.getElementById(strListClientID).value;
var hiddenField = document.getElementById(strHiddenFieldClientID);
hiddenField.value = strJobTitle;
<asp:HiddenField ID="hfJobTitle" runat="server" />
***dropdown in template field***
<asp:DropDownList ID="lstJobTitles" runat="server" DataSourceID="dsJobTitles"
DataTextField="Title" DataValueField="Title" OnPreRender="lstJobTitles_PreRender" ></asp:DropDownList>
protected void lstJobTitles_PreRender(object sender, EventArgs e)
DropDownList ddlist = sender as DropDownList;
string strListClientID = ddlist.ClientID;
string strHiddenFieldClientID = hfJobTitle.ClientID;
ddlist.Attributes.Add("onchange", "jobTitleToHiddenField(’" + strListClientID + "’, ‘" + strHiddenFieldClientID + "’);");
then you can get the value in the code behind anywhere you need it with…
string strTitle = hfJobTitle.Value;