DropDownList选中的值

时间:2016-01-13 20:54:43

标签: c# asp.net gridview drop-down-menu

嗨,我在下面的下拉列表中有这个。

<asp:DropDownList ID="dllcust" CssClass="form-control chosen-select" runat="server" AutoPostBack="True" DataSourceID="Sqls_Cust" DataTextField="Cust_Name" DataValueField="Cust_ID" ></asp:DropDownList>
<asp:SqlDataSource ID="Sqls_Cust" runat="server" ConnectionString="<%$ ConnectionStrings:ConnString %>" SelectCommand="SELECT [Cust_ID], [Cust_Name] FROM [Customer]"></asp:SqlDataSource>

<asp:DropDownList ID="dllinvoivceid" CssClass="form-control chosen-select" runat="server"  DataSourceID="Sqlds_Invoive" DataTextField="InvoiceID"  DataValueField="InvoiceID">
</asp:DropDownList>

<asp:SqlDataSource ID="Sqlds_Invoive" runat="server" ConnectionString="<%$ ConnectionStrings:ConnString %>" SelectCommand="SELECT Invoice.InvoiceID FROM Invoice INNER JOIN Customer ON Invoice.Cust_ID = Customer.Cust_ID WHERE (Customer.Cust_ID = @Cust_ID)">
<SelectParameters>
<asp:ControlParameter ControlID="dllcust" Name="Cust_ID" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>

正如您从代码dllinvoivceid中看到的那样,根据dllcust值获取值,以便我可以为所选客户获取发票。

我的网格视图定义如下:

<asp:GridView ID="gridvoucher" OnRowCommand="gridvoucher_RowCommand" runat="server" AutoGenerateColumns="false" CssClass="table table-bordered text-nowrap">
<Columns>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="LinkButton8" runat="server" CausesValidation="False" CommandName="EditVoucher" CssClass="btn btn-primary btn-xs" Text=""><i class="glyphicon glyphicon-pencil"></i></asp:LinkButton>
</ItemTemplate>
<ControlStyle CssClass="btn btn-primary" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="LinkButton9" runat="server" CausesValidation="False" CommandName="DeleteVoucher" CssClass="btn btn-primary btn-xs" OnClientClick="return confirm('Are you sure you want to delete this record ?');" Text="Delete"><i class="glyphicon glyphicon-trash"></i></asp:LinkButton>
</ItemTemplate>
<ControlStyle CssClass="btn btn-danger" />
</asp:TemplateField>
<asp:BoundField DataField="Voucher ID" HeaderText="Voucher #" SortExpression="Voucher ID" />
<asp:BoundField DataField="Invoice ID" HeaderText="Invoice ID" SortExpression="Invoice ID" />
<asp:BoundField DataField="Cust_ID" HeaderText="Cust_ID" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" SortExpression="Cust_ID" />
<asp:BoundField DataField="Customer" HeaderText="Customer Name" SortExpression="Customer Name" />
</Columns>
<EmptyDataTemplate>
&quot;No records found&quot;
</EmptyDataTemplate>
</asp:GridView>

gridvoucher_RowCommand我有以下代码:

protected void gridvoucher_RowCommand(object sender, GridViewCommandEventArgs e)
{

    string commandName = e.CommandName.ToString().Trim();
    GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
    string voucherID = row.Cells[2].Text;
    string custid = row.Cells[4].Text;
    string invoiceid = row.Cells[3].Text;
    GridViewRow gvRow = (GridViewRow)((Control)e.CommandSource).NamingContainer;
    Int32 rowind = gvRow.RowIndex;
    switch (commandName)
    {
        case "EditVoucher":
            txtvoucherid.Text=voucherID;
            dllcust.SelectedValue = custid;
            dllinvoivceid.SelectedValue = invoiceid;
            MultiView1.ActiveViewIndex = 1;
            break;
    }
}

问题是标识为dllinvoivceid的下拉列表,它不会从网格视图中选择所需的值

它从Sqlds_Invoive的select命令获取第一条记录。

1 个答案:

答案 0 :(得分:1)

从两个下拉列表中的每个下拉列表中删除标记中的DataSourceID属性,并订阅SelectIndexChanged事件dllcust,其中您将数据绑定dllinvoiceid下拉列表。 dllcust下拉列表需要在Page_Load事件中绑定数据。如果您这样做,网格视图的RowCommand事件中设置的选定值将被正确设置。

从标记中删除DataSourceID属性以获取下拉列表

<asp:DropDownList ID="dllcust" CssClass="form-control chosen-select" runat="server"
     AutoPostBack="True" OnSelectedIndexChanged="dllcust_SelectedIndexChanged"
     DataTextField="Cust_Name" DataValueField="Cust_ID" ></asp:DropDownList>
<asp:SqlDataSource ID="Sqls_Cust" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnString %>" SelectCommand="SELECT [Cust_ID], [Cust_Name] FROM [Customer]"></asp:SqlDataSource>

<asp:DropDownList ID="dllinvoivceid" CssClass="form-control chosen-select" runat="server"
       DataTextField="InvoiceID"  DataValueField="InvoiceID">
</asp:DropDownList>

<asp:SqlDataSource ID="Sqlds_Invoive" runat="server"
 ConnectionString="<%$ ConnectionStrings:ConnString %>" 
 SelectCommand="SELECT Invoice.InvoiceID FROM Invoice INNER JOIN 
      Customer ON Invoice.Cust_ID = Customer.Cust_ID WHERE (Customer.Cust_ID = @Cust_ID)">
<SelectParameters>
<asp:ControlParameter ControlID="dllcust" Name="Cust_ID" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>

数据使用DataSource属性绑定代码隐藏中的下拉列表

    protected void Page_Load(object sender, EventArgs e)
    {
          if(!Page.IsPostBack) 
            {
               dllcust.DataSource = this.Sqls_Cust;
               dllcust.Databind();
            }
    }

    protected void ddlcust_SelectedIndexChanged(object sender, EventArgs e)
    {
               dllinvoiceId.DataSource = this.Sqlds_Invoive;
               dllinvoiceId.Databind();
    }