在ASP.Net中从DDL中检索代码隐藏的值 [英] Retrieving value from DDL for Code Behind in ASP.Net

查看:128
本文介绍了在ASP.Net中从DDL中检索代码隐藏的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从我的ASP页面上的下拉列表中提取值。



这是页面本身的标记:



 < span class =code-keyword><   asp:DropDownList     ID   =  ddlCurrentIncidentStatus    runat   =  server    高度  = < span class =code-keyword> 37px    SelectedValue   ='  <% #Bind(   Current_Incident_Status)%>'   宽度  =  347像素 >  
< asp:ListItem > 请选择一个... < / asp:ListItem >
< asp:ListItem > 包含< / asp:ListItem >
< ; asp:ListItem > 未包含的< / asp:ListItem >
< span class =code-keyword>< asp:ListItem > 调查< span class =code-keyword>< / asp:ListItem >
< asp:ListItem > 其他(在事件描述中解释)< / asp:ListItem >
< / asp:DropDownList >





这是c的代码背后ontrol:



 DropDownList CurrentIncidentStatus =(DropDownList)FormView1.FindControl(  ddlCurrentIncidentStatus); 
string currentIncidentStatus = CurrentIncidentStatus.Text.ToString();





我收到此错误:



无法将类型为'System.Web.UI.WebControls.DropDownList'的对象强制转换为'System.Web.UI' .WebControls.TextBox'

解决方案





我认为DropDownList没有文字属性,按照以下修改为SelectedValue。



 DropDownList CurrentIncidentStatus =(DropDownList)FormView1.FindControl(  ddlCurrentIncidentStatus); 
string currentIncidentStatus = CurrentIncidentStatus.SelectedValue;





希望它帮助。


(DropDownList)FormView1.FindControl(ddlCurrentIncidentStatus); 不会抛出

< blockquote class =FQ>

Quote:

无法将类型为'System.Web.UI.WebControls.DropDownList'的对象强制转换为'System.Web.UI.WebControls.TextBox '



错误在其他一些代码上,检查所有找到控件和转换的placess。例如,您可能搜索DropDownList并转换为TextBox,如(TextBox)FormView1.FindControl(ddl ....

查找和纠正成本核算。


Trying to pull a value out of a Drop Down List on my ASP page.

Here is the markup for the page itself:

<asp:DropDownList ID="ddlCurrentIncidentStatus" runat="server" Height="37px" SelectedValue='<%# Bind("Current_Incident_Status") %>' Width="347px">
                                    <asp:ListItem>Please select one...</asp:ListItem>
                                    <asp:ListItem>Contained</asp:ListItem>
                                    <asp:ListItem>Uncontained</asp:ListItem>
                                    <asp:ListItem>Investigating</asp:ListItem>
                                    <asp:ListItem>Other (Explained in Incident Description)</asp:ListItem>
                                </asp:DropDownList>



Here is the Code Behind for the control:

DropDownList CurrentIncidentStatus = (DropDownList)FormView1.FindControl("ddlCurrentIncidentStatus");
                string currentIncidentStatus = CurrentIncidentStatus.Text.ToString();



I'm getting this error:

Unable to cast object of type 'System.Web.UI.WebControls.DropDownList' to type 'System.Web.UI.WebControls.TextBox'

解决方案

Hi,

I don't think a DropDownList has a Text property, modify it to SelectedValue as per below.

DropDownList CurrentIncidentStatus = (DropDownList)FormView1.FindControl("ddlCurrentIncidentStatus");
string currentIncidentStatus = CurrentIncidentStatus.SelectedValue;



Hope it helps.


(DropDownList)FormView1.FindControl("ddlCurrentIncidentStatus"); will not throw

Quote:

Unable to cast object of type 'System.Web.UI.WebControls.DropDownList' to type 'System.Web.UI.WebControls.TextBox'


Error is on some other code, check all placess where you find controls and casting. for example, you may have search for DropDownList and casting to TextBox like (TextBox)FormView1.FindControl("ddl....
Find and correct the costing.


这篇关于在ASP.Net中从DDL中检索代码隐藏的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆