Monday, 15 September 2014

asp.net - Adding Dropdown List Item to ASP GridView -



asp.net - Adding Dropdown List Item to ASP GridView -

i want able take selected item dropdown list, nail button, , have item added gridview viewed user. right now, when nail + button, grid displays, cells blank. suggestions?

asp code:

<tr> <td valign="top" colspan="2"> <b>agents visited</b><br /> <asp:dropdownlist selectionmode="multiple" runat="server" id="agentsdropdown" name="agentsdropdown" width="425"></asp:dropdownlist> </td> <td valign="top"> <br /> <asp:button id="agentbutton" name="agentbutton" runat="server" text="+" onclick="addagent" cssclass="buttonstyle" onmouseover="shade(this);" onmouseout="unshade(this);" /> </td> </tr> <tr> <asp:gridview id="agentgridview" visible="false" allowsorting="false" allowpaging="false" runat="server" autogeneratecolumns="false" pagesize="20" > <columns> <asp:templatefield headertext="agent"> <itemtemplate> <asp:label id="agentname" runat="server" text=''></asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="value"> <itemtemplate> <asp:label id="agentvalue" runat="server" text=''></asp:label> </itemtemplate> </asp:templatefield> <asp:commandfield showdeletebutton="true" /> </columns> </asp:gridview> </tr>

initial gridview bind:

private sub page_load(byval sender object, byval e system.eventargs) handles me.load if not page.ispostback agentgridview.datasource = nil agentgridview.databind() end if end sub

additional code behind:

protected sub addagent(sender object, e eventargs) if agentsdropdown.selectedindex > 0 dim dt new datatable dt.columns.add("agentname") dt.columns.add("agentvalue") dim row1 datarow = dt.newrow row1.item("agentname") = agentsdropdown.selecteditem.text.tostring() row1.item("agentvalue") = agentsdropdown.selectedvalue.tostring() dt.rows.add(row1) agentgridview.datasource = dt agentgridview.databind() agentsdropdown.selectedindex = 0 dim agentrowscount integer = agentgridview.rows.count if agentrowscount > 0 agentgridview.visible = true end if end if end sub

this should work now. alter gridview following:

<asp:gridview id="agentgridview" visible="false" allowsorting="false" allowpaging="false" runat="server" autogeneratecolumns="false" pagesize="20" > <columns> <asp:boundfield datafield="agentname" headertext="agent name" itemstyle-width="30" /> <asp:boundfield datafield="agentvalue" headertext="agent value" itemstyle-width="30" /> <asp:commandfield showdeletebutton="true" /> </columns> </asp:gridview>

asp.net vb.net

No comments:

Post a Comment