Seleted索引在下拉列表中没有变化 [英] Seleted index is not changing in dropdownlist
问题描述
我有一个看似很常见的问题,但我无法解决。我正在使用C#,visual studio 2013进行Web表单项目。
我使用了dropownlist来选择报告类型。我使用了3个面板,这些面板位于updatepanel中,下拉列表位于Updatepanel之外。最初我将每个面板的visible属性设置为false。当用户从下拉列表中选择项目时,我想让其中一个面板可见。问题是当我在下拉列表中更改选择时,SelectedIndexChanged事件将触发,但所选索引每次都保持-1。我已将autopostback设置为true。以下是我的代码。
< asp:DropDownList ID = ddlReportType AutoPostBack = true runat = server OnSelectedIndexChanged = ddlReportType_SelectedIndexChanged >
< asp:ListItem Selected = True> - 选择您要输入的数据 - < / asp:ListItem >
< asp:ListItem Value = Salse Text = Salse > Salse报告< / asp:ListItem >
< asp:ListItem Value = < span class =code-string> 库存文本= 库存>库存报告< / asp:ListItem >
< asp:ListItem Value = 财务文本= 财务>财务报告< / asp:ListItem >
< / asp:DropDownList >
< br />
< asp:标签ID = Label1 runat = server Text = > < / asp:标签 >
< br />
< asp:UpdatePanel ID = UpdatePanel1 runat = server >
< ContentTemplate>
< asp:Panel ID = pnlProductSale runat = server >
< asp:标签ID = lblCaptionSales runat = server Text = 总产品销售 > < / asp:标签 >
< table>
< tr>
< td>
选择产品
< / td >
< td>
< asp:DropDownList ID = ddlProduct runat = server > < / asp:DropDownList >
< / td >
< / tr >
< span class =code-keyword>< / table >
< / asp:Panel >
< ; asp:Panel ID = pnlInventory runat = server >
< asp:Label ID = lblInventory runat = < span class =code-string> server Text = 库存 > < / asp:Label >
< table>
< tr>
< td>
选择产品
< / td >
< td>
< asp:DropDownList ID = ddlProductPnl2 runat = server > < / asp:DropDownList >
< / td >
< / tr >
< / 表格 >
< / asp:Panel >
< ; asp:Panel ID = pnlCashFlow runat = server >
< asp:Label ID = lblCaption runat = server Text = Cashflow > < / asp:Label >
< table>
< tr>
< td>
启动时存款
< / td >
< td>
手头现金:< asp:TextBox ID = txtStartAmt Text = 0 AutoPostBack = true runat = server > < / asp:TextBox >
Credit:< asp:TextBox ID = txtStartCredit Text = 0 AutoPostBack = true runat = server > < / asp:TextBox >
借方:< asp:TextBox ID = txtstartDebit Text = 0 AutoPostBack = true runat = server > < / asp:TextBox >
< / td >
< / tr >
< TR>
< td>
< asp:ImageButton ID = imgSave runat = server />
< / td >
< td>
< asp:ImageButton ID = imgCancel runat = server />
< / td >
< / tr >
< / table >
< / asp:Panel >
< / ContentTemplate >
<触发器>
< asp:AsyncPostBackTrigger ControlID = ddlReportType />
< / 触发器 >
< / asp:UpdatePanel >
以下是.cs文件中的代码。
protected void Page_Load( object sender,EventArgs e)
{
if (Page.IsPostBack == false )
{
pnlCashFlow.Visible = false ;
pnlProductSale.Visible = false ;
pnlInventory.Visible = false ;
}
}
受保护 void ddlReportType_SelectedIndexChanged( object sender,EventArgs e)
{
if (ddlProduct.SelectedIndex == 0 )
{
Label1.Text = ddlProduct.SelectedValue;
pnlCashFlow.Visible = false ;
pnlProductSale.Visible = false ;
pnlInventory.Visible = true ;
}
else if (ddlProduct.SelectedIndex == 1 )
{
Label1.Text = ddlProduct.SelectedValue;
pnlCashFlow.Visible = false ;
pnlProductSale.Visible = true ;
pnlInventory.Visible = false ;
}
else if (ddlProduct.SelectedIndex == 2 )
{
Label1.Text = ddlProduct.SelectedValue;
pnlCashFlow.Visible = true ;
pnlProductSale.Visible = false ;
pnlInventory.Visible = false ;
}
else
{
Label1.Text = ddlProduct.SelectedValue;
pnlCashFlow.Visible = true ;
pnlProductSale.Visible = false ;
pnlInventory.Visible = false ;
}
}
请帮我解决这个问题。还告诉我,如果我使用了错误的方法。
是的。你犯了很大的错误。您正在处理下面的事件处理程序来处理ddReportType选择的更改事件ddlReportType_SelectedIndexChanged
但是你正在使用
ddlProduct.SelectedIndex
事件处理程序代码。
Hi, I have a problem that seems very common but I can't solve it. I am doing web forms project with C#, visual studio 2013.
I have used a dropownlist to select type of report. I have used 3 panels which are in updatepanel and the dropdownlist is outside of the Updatepanel. Initially I set visible property of each panel to false. I want to make one of the panels visible when user selects item from dropdownlist. The problem is when I change selection in dropdownlist, the SelectedIndexChanged event fires, but the selected index remains -1 every time. I have set autopostback to true. Following is my code.
<asp:DropDownList ID="ddlReportType" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ddlReportType_SelectedIndexChanged" >
<asp:ListItem Selected="True">-- Select which data you want to enter --</asp:ListItem>
<asp:ListItem Value="Salse" Text="Salse">Salse Reports</asp:ListItem>
<asp:ListItem Value="Inventory" Text="Inventory">Inventory Reports</asp:ListItem>
<asp:ListItem Value="Finance" Text="Finance">Financial Reports</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="pnlProductSale" runat="server">
<asp:Label ID="lblCaptionSales" runat="server" Text="Total Product based sale"></asp:Label>
<table>
<tr>
<td>
Select Product
</td>
<td>
<asp:DropDownList ID="ddlProduct" runat="server"></asp:DropDownList>
</td>
</tr>
</table>
</asp:Panel>
<asp:Panel ID="pnlInventory" runat="server">
<asp:Label ID="lblInventory" runat="server" Text="Inventory"></asp:Label>
<table>
<tr>
<td>
Select Product
</td>
<td>
<asp:DropDownList ID="ddlProductPnl2" runat="server"></asp:DropDownList>
</td>
</tr>
</table>
</asp:Panel>
<asp:Panel ID="pnlCashFlow" runat="server">
<asp:Label ID="lblCaption" runat="server" Text="Cashflow"></asp:Label>
<table>
<tr>
<td>
Deposit at start up
</td>
<td>
Cash on hand : <asp:TextBox ID="txtStartAmt" Text="0" AutoPostBack="true" runat="server" ></asp:TextBox>
Credit : <asp:TextBox ID="txtStartCredit" Text="0" AutoPostBack="true" runat="server" ></asp:TextBox>
Debit : <asp:TextBox ID="txtstartDebit" Text="0" AutoPostBack="true" runat="server" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:ImageButton ID="imgSave" runat="server" />
</td>
<td>
<asp:ImageButton ID="imgCancel" runat="server" />
</td>
</tr>
</table>
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlReportType" />
</Triggers>
</asp:UpdatePanel>
following is the code in .cs file.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
pnlCashFlow.Visible = false;
pnlProductSale.Visible = false;
pnlInventory.Visible = false;
}
}
protected void ddlReportType_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlProduct.SelectedIndex == 0)
{
Label1.Text = ddlProduct.SelectedValue;
pnlCashFlow.Visible = false;
pnlProductSale.Visible = false;
pnlInventory.Visible = true;
}
else if (ddlProduct.SelectedIndex ==1)
{
Label1.Text = ddlProduct.SelectedValue;
pnlCashFlow.Visible = false;
pnlProductSale.Visible = true;
pnlInventory.Visible = false;
}
else if (ddlProduct.SelectedIndex==2)
{
Label1.Text = ddlProduct.SelectedValue;
pnlCashFlow.Visible = true;
pnlProductSale.Visible = false;
pnlInventory.Visible = false;
}
else
{
Label1.Text = ddlProduct.SelectedValue;
pnlCashFlow.Visible = true;
pnlProductSale.Visible = false;
pnlInventory.Visible = false;
}
}
Please help me to solve this. Also tell me if I am using the wrong approach.
Yes. Very big mistake you did. You are handling below event handler to handle ddReportType selected change eventddlReportType_SelectedIndexChanged
But you are using
ddlProduct.SelectedIndex
in the event handler code.
这篇关于Seleted索引在下拉列表中没有变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!