Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.NET
<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
C++
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

JavaScript
<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
Posted
Comments
ramyajaya 1-Apr-15 17:18pm    
Please mention your issue in steps .

And please provide your exact view cide in the code you have guven I didnt find listview close tag and item template close tag and there is td tag inside item template why that is needed .

Please let us know your objective

1 solution

Give a cssclass to DropdownList like CssClass="items" and CssClass="itemsLabel" for Label2

ASP.NET
<asp:dropdownlist id="DropDownList2" runat="server" autopostback="True" datasourceid="Folio" datatextfield="Folio" datavaluefield="Folio" cssclass="items" xmlns:asp="#unknown">

  <asp:label id="Label2" runat="server" text="<%# Bind("Folio") %>" visible="False" cssclass="itemsLabel"></asp:label>


</asp:dropdownlist>




Then write Javascript:



$('.items').change(function () {
val = ($('#items').find('option:selected').val();

$('.items').closest('tr').('.itemsLabel').text(val);
});
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900