DropDownList selected value
本文关键字:value selected DropDownList | 更新日期: 2023-09-27 18:30:09
嗨,我有下面的下拉列表。
<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>
"No records found"
</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命令中获取第一条记录。
为两个下拉列表中的每一个从标记中删除DataSourceID属性,并订阅dllcust
的SelectIndexChanged
事件,在该事件中数据绑定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();
}