【问题标题】:DropDownList selected valueDropDownList 选定值
【发布时间】:2016-04-19 00:37:51
【问题描述】:

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

<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;
    }
}

问题是 ID 为 dllinvoivceid 的下拉列表没有从网格视图中获取所选值

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

【问题讨论】:

  • 不要在标记中绑定客户和发票下拉菜单,而是在代码隐藏中使用如下示例:ddlCust.DataSource = sqDataSource1; ddlCust.DataBind()。此外,如果 Page.IsPostBack == false,请在 Page_Load 事件中执行此代码隐藏数据绑定。不要使用DataSourceID 属性,因为这是您的问题的原因。

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


【解决方案1】:

从您的标记中删除两个下拉列表中的每一个的 DataSourceID 属性,并订阅 dllcustSelectIndexChanged 事件,您将在其中数据绑定 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();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多