简体   繁体   English

DropDownList选择值问题

[英]DropDownList selected value issue

The below code gets selected value from a grid-view row to DropDownList with id dllinvoivceid 下面的代码将选定的值从网格视图行获取到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);
}
}

as you can see from below code i put constrain for dllinvoivceid based on dllcust to get invoices for only the selected customer the issue is the selected value i get for dllinvoivceid is the first result from the select command in dllinvoivceid not the value from the grid view 你可以从下面的代码看到我把压抑了dllinvoivceid基于dllcust拿到发票只有被选中的客户的问题是选定值我得到dllinvoivceid是在SELECT命令的第一个结果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>

您应该使用SelectedIndex即

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM