Hello everybody
I'm trying to make a website on ASP.NET (VS2013 Express), in this website users can insert data to a MSSQL DB.
I'm using Insert Item Template from ListView, but i'm trying to reduce user mistakes introducing dropdownlist (it's a relational DB). I can make Cascading Dropdownlist but for some strange reason property SelectedValue=<%# Bind("Field1") %> does not work for me.
I Googled about and i find a way taking the Selected Value (with Autopostback enabled) and moving data into a label with Text =<%# Bind("Field1") %>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="sequence" DataSourceID="Table3ID" GroupItemCount="3" InsertItemPosition="FirstItem">
<InsertItemTemplate>
<td runat="server" style="">
Folio:
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Folio") %>' Visible="False"></asp:Label>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" DataSourceID="Folio" DataTextField="Folio" DataValueField="Folio">
</asp:DropDownList>
<asp:SqlDataSource ID="Folio" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
SelectCommand="SELECT * FROM Table2">
</asp:SqlDataSource>
<br />Asignado:
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Asignado") %>' Visible="True"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="Asignado" DataTextField="Nombre" DataValueField="sequence" >
</asp:DropDownList>
<asp:SqlDataSource ID="Asignado" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
SelectCommand="SELECT * FROM Table1">
</asp:SqlDataSource>
<br />
and some behind code to enable javascript
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(GetType(),"obtieneAsignado","ddl1();", true);
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "obtieneFolio", "ddl2();", true);
}
And some JavaScript code
<script type="text/javascript">
function ddl1() {
document.getelementbyid('<%=ListView1.FindControl("Label1").ClientID%>').Value = document.getelementbyID('<%=ListView1.FindControl("DropDownList1").ClientID%>').SelectedValue;
}
function ddl2() {
document.getelementbyid('<%=ListView1.FindControl("Label2").ClientID%>').Value = document.getelementbyID('<%=ListView1.FindControl("DropDownList2").ClientID%>').SelectedValue;
if (<%=ListView1.FindControl("DropDownList2").ClientID%> == null || '<%=ListView1.FindControl("DropDownList1").ClientID%>' == null) return;
}
</script>
Well, really it's a kind of Frankenstein solution cause i take pieces of code from lots of sites. The trouble here is no matter the way i take, i can't make the insert works. Apparently the Javascript doesn't work because the objects not exist, i get the error:
Object reference not set to an instance of an object.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Code Error:
Línea 9: <script type="text/javascript">
Línea 10: function ddl1() {
Línea 11: document.getelementbyid('<%=ListView1.FindControl("Label1").ClientID%>').Value = document.getelementbyID('<%=ListView1.FindControl("DropDownList1").ClientID%>').SelectedValue;
Línea 12: }
Línea 13: function ddl2() {
I appreciate your help