DropDownList所选值问题

本文关键字:问题 DropDownList | 更新日期: 2023-09-27 18:29:45

下面的代码将网格视图行中的选定值获取到id为dllinvoivceid 的DropDownList

protected void gridvoucher_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
string commandName = e.CommandName.ToString().Trim();
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);             
string custid = row.Cells[5].Text;
string invoiceid = row.Cells[4].Text;
GridViewRow gvRow = (GridViewRow)((Control)e.CommandSource).NamingContainer;
Int32 rowind = gvRow.RowIndex;
switch (commandName)
{
case "EditVoucher":
dllcust.SelectedValue = custid;
dllinvoivceid.SelectedValue = invoiceid;                       
MultiView1.ActiveViewIndex = 1;
break;                   
}
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
}
}

正如你从下面的代码中看到的,我基于dllcustdllinvoivceid进行了约束,只为选定的客户获取发票,问题是我为dllinvoivceid获得的选定值是dllinvoivceid中选择命令的第一个结果,而不是网格视图的值

<div class="form-group">
<label for="InputName">
Customer Name</label>
<div class="form-group">
<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>
</div>
</div>
<div class="form-group">
<label for="InputName">
Invoice ID</label>
<div class="form-group">
<asp:DropDownList ID="dllinvoivceid" CssClass="form-control chosen-select" runat="server" AutoPostBack="True" DataSourceID="Sqlds_Invoive" DataTextField="InvoiceID" DataValueField="InvoiceID"></asp:DropDownList>
<asp:SqlDataSource ID="Sqlds_Invoive" runat="server" ConnectionString="<%$ ConnectionStrings:ConnString %>" SelectCommand="SELECT Invoice.InvoiceID FROM Customer INNER JOIN Invoice ON Customer.Cust_ID = Invoice.Cust_ID WHERE (Customer.Cust_ID = @Cust_ID)">
<SelectParameters>
<asp:ControlParameter ControlID="dllcust" Name="Cust_ID" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</div>
</div>

DropDownList所选值问题

您应该使用SelectedIndex,即

dllinvoivceid.SelectedIndex=dllinvoivceid.Items.IndexOf(ddlData.Items.FindByText(invoiceid));