以下代码从网格视图行中获取选定值到标识为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);
}
}
正如您从下面的代码中看到的那样,我根据dllinvoivceid
对dllcust
进行约束以仅为所选客户获取发票,问题是我为dllinvoivceid
获取的选定值是第一个结果来自dllinvoivceid
中的select命令而不是来自网格视图的值
<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>
答案 0 :(得分:1)
您应该使用SelectedIndex,即
dllinvoivceid.SelectedIndex=dllinvoivceid.Items.IndexOf(ddlData.Items.FindByText(invoiceid));