我在gridview中有ddl,它与gridview的其余部分分开绑定。我想基于ddl更新sql数据库,但是我收到错误,“无法在ControlParameter中找到控件'ddlUsers'。”
Public Sub BindGridView()
'bind gridview
SqlDataSource1.SelectCommand = "Select * From Leadership Left Outer Join Customer On Leadership.CustomerId = Customer.CustomerId"
SqlDataSource1.DataBind()
'binding dropdownlist inside of gridview
SqlDataSource2.SelectCommand = "SELECT LName + ', ' + FName As Name, CustomerId From Customer Where Active = 'True' And Probation = 'False' Order by LName ASC"
SqlDataSource2.DataBind()
End Sub
Protected Sub GridView1_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
Dim ddl As DropDownList = DirectCast(GridView1.Rows(e.RowIndex).FindControl("ddlUsers"), DropDownList)
Dim conStr As String = "Data Source=mydatasource"
Dim cn As New SqlConnection(conStr)
cn.Open()
Dim updCmd As New SqlCommand("updateLeadership", cn)
updCmd.CommandType = CommandType.StoredProcedure
updCmd.Parameters.Add("@CustomerId", SqlDbType.Int).Value = ddl.SelectedValue.ToString()
updCmd.Parameters.Add("@Position", SqlDbType.NVarChar).Value = ddl.SelectedItem.ToString()
SqlDataSource1.DataBind()
updCmd.Parameters.Clear()
bindGridView()
End Sub
然后是gridview
<asp:GridView id="GridView1" runat="server" OnRowUpdating="GridView1_RowUpdating" DataSourceID="SqlDataSource1" DataKeyNames="Id">
<Columns>
<asp:TemplateField>
<HeaderStyle Width="5%" />
<ItemStyle Width="5%" />
<EditItemTemplate>
<asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
<asp:LinkButton ID="btnUpdate" Runat="server" CommandName="Update" OnClientClick="return confirm('Are you sure you're sure you want to update this position?');">Update</asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" Runat="server" OnClientClick="return confirm('Are you sure you want to update this position?');"
CommandName="Edit">change</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Position" >
<ItemTemplate>
<asp:Label runat="server" ID="President" Text ='<%# Eval("Position")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Member Name" >
<EditItemTemplate>
<asp:dropdownlist id="ddlUsers" runat="server" DataSourceID="SqlDataSource2" AppendDataBoundItems="true" DataTextField="Name" DataValueField="CustomerId" AutoPostBack="true"></asp:dropdownlist>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="lblFirstVice" Text ='<%# Eval("FName") & " " & Eval("LName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
</asp:GridView>
<asp:SqlDataSource id="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NCOAConnectionString %>" UpdateCommand="UpdateLeadership" UpdateCommandType="StoredProcedure">
<UpdateParameters>
<asp:controlparameter controlid="ddlUsers" propertyname="SelectedValue" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource id="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NCOAConnectionString %>">
</asp:SqlDataSource>