c# - Use a FOR loop to return SelectedValue of DropDownList -
i've been spending lot of time searching solution seems simple thing.
the following returns "object reference not set instance of object." error. put code on separate blank page troubleshooting. ideas?
asp.net:
<div> <asp:dropdownlist id="choosepattern1" clientidmode="static" runat="server" cssclass="dropformat" autopostback="true" onselectedindexchanged="getimage"> <asp:listitem>1</asp:listitem> <asp:listitem>2</asp:listitem> <asp:listitem>3</asp:listitem> </asp:dropdownlist> <asp:dropdownlist id="choosepattern2" clientidmode="static" runat="server" cssclass="dropformat" autopostback="true" onselectedindexchanged="getimage"> <asp:listitem>1</asp:listitem> <asp:listitem>2</asp:listitem> <asp:listitem>3</asp:listitem> </asp:dropdownlist> </div>
c# codebehind:
protected void getimage(object sender, eventargs e) { (int intcounter = 1; intcounter <= 8; intcounter++) { string mypattern = "choosepattern" + convert.tostring(intcounter); dropdownlist ddl = page.findcontrol(mypattern) dropdownlist; response.write(ddl.selectedvalue); } }
that error message called "null reference exception". means you're trying access property object, object null.
in case, ddl
problem. if it's not found page.findcontrol
(or if returned object not dropdownlist
), value stored null. therefore, accessing selectedvalue
cause exception.
Comments
Post a Comment