我希望能够从下拉列表中选择一个项目,点击一个按钮,并将该项目添加到GridView以供用户查看。现在,当我按下+按钮时,网格显示,但是单元格是空白的。有什么建议吗?
ASP代码:
<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>
初始GridView绑定:
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack
agentGridView.DataSource = Nothing
agentGridView.Databind()
End If
End Sub
背后的附加代码:
Protected Sub AddAgent(sender As Object, e As EventArgs)
If agentsDropdown.SelectedIndex > 0 Then
Dim dt As New DataTable
dt.Columns.Add("agentName")
dt.Columns.Add("agentValue")
Dim row1 As 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 as Integer = agentGridView.Rows.Count
If agentRowsCount > 0
agentGridView.Visible = True
End If
End If
End Sub
答案 0 :(得分:1)
这应该适合你。将GridView更改为以下内容:
<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>
答案 1 :(得分:1)
看看这是否适合你。我更喜欢它#然而这看起来很有效:
Public Class WebForm1
Inherits System.Web.UI.Page
Dim dt = Nothing
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Session("Agents") Is Nothing) Then
dt = New DataTable
Else
dt = Session("Agents")
End If
If Not Page.IsPostBack Then
agentGridView.DataSource = Nothing
agentGridView.DataBind()
End If
End Sub
Protected Sub AddAgent(sender As Object, e As EventArgs) Handles agentsDropdown.SelectedIndexChanged
If agentsDropdown.SelectedIndex > 0 Then
If (Session("Agents") Is Nothing) Then
dt = New DataTable()
dt.Columns.Add("agentName")
dt.Columns.Add("agentValue")
End If
Dim row1 As DataRow = dt.NewRow
row1.Item("agentName") = agentsDropdown.SelectedItem.Text.ToString()
row1.Item("agentValue") = agentsDropdown.SelectedValue.ToString()
dt.Rows.Add(row1)
Session("Agents") = dt
agentGridView.DataSource = dt
agentGridView.DataBind()
agentsDropdown.SelectedIndex = 0
Dim agentRowsCount As Integer = agentGridView.Rows.Count
If agentRowsCount > 0 Then
agentGridView.Visible = True
End If
End If
End Sub
End Class