下拉列表问题
我不明白为什么我无法获取 ddl 选择的值..
ASP 代码:
<table>
<tr>
<th> Annee:</th>
<td>
<asp:DropDownList ID="ddlAnnee" runat="server" />
</td>
</tr>
<tr>
<th colspan="2">
<asp:Button ID="btnValidate" runat="server" onclick="btnValidate_Click"
Text="Validate" />
</th>
</tr>
</table>
代码后面:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindControls();
}
}
public void BindControls()
{
this.ddlAnnee.DataSource = new BLL.ANNEE_MANAGER().List(false, true, null);
this.ddlAnnee.DataTextField = "INTITULE_AN";
this.ddlAnnee.DataValueField = "ID_AN";
this.ddlAnnee.DataBind();
}
protected void btnValidate_Click(object sender, EventArgs e)
{
int Id_an = int.Parse( this.ddlAnnee.SelectedValue);
}
}
}
所以,当我在页面加载和绑定部分放置断点时,没关系,ddl 已正确填充并且页面正确显示 ddl。当我单击按钮时,我到达 btnValidate_Click 方法,但 ddl 是空的!
我想我忘记了什么......请帮助我!
谢谢..
I don't understand why I can't get the ddl selected value..
ASP CODE:
<table>
<tr>
<th> Annee:</th>
<td>
<asp:DropDownList ID="ddlAnnee" runat="server" />
</td>
</tr>
<tr>
<th colspan="2">
<asp:Button ID="btnValidate" runat="server" onclick="btnValidate_Click"
Text="Validate" />
</th>
</tr>
</table>
Code BEHIND:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindControls();
}
}
public void BindControls()
{
this.ddlAnnee.DataSource = new BLL.ANNEE_MANAGER().List(false, true, null);
this.ddlAnnee.DataTextField = "INTITULE_AN";
this.ddlAnnee.DataValueField = "ID_AN";
this.ddlAnnee.DataBind();
}
protected void btnValidate_Click(object sender, EventArgs e)
{
int Id_an = int.Parse( this.ddlAnnee.SelectedValue);
}
}
}
So, when i put a break point at the page load and binding part, it's ok, the ddl is filled correctly and the page show correctly the ddl. When I click on the button, I arrive in the btnValidate_Click method but the ddl is empy!
I suppose I've forgot something.. Please help me!
Thanks..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要删除 IsPostBack,BindControl 方法应该处于 !IsPostBack 条件下,否则您永远不会获得选定的值。只需确保任何方法都不会清除 ddlAnnee 下拉列表。
Do not remove IsPostBack, the BindControl method should be in !IsPostBack Condition otherwise you are never going to get selected value. Just make sure that any of the method not clearing the ddlAnnee dropdown.
删除
IsPostBack
检查 - 当您单击按钮时,代码不会重新填充下拉列表,因此它是空的并且所选值不再存在。Remove the
IsPostBack
check - when you click the button the code is not refilling the drop down so it is empty and the selected value no longer exists.您必须在新页面加载和回发时绑定网格。
You have to bind the grid both on a fresh page load and on a postback.
我认为,当您单击按钮时,将执行页面加载,并且您会在单击时或执行子操作后对 ddl 中的所有数据进行校准,因此,当执行单击事件并且您已将 bindcontrol() 置于 ISPOSTBACK 要求下时,ddl 将执行以下操作:页面加载时第一次绑定,因此根据要求更改代码。
i Think That when you click the button then page load performed and you calearing the all the data from ddl at the click or after performing son action so when click event performed and you had put the bindcontrol() under the ISPOSTBACK requirements ddl are going to bind the first time when page is load so change the code according the requirements.