我有个.aspx页面 这个页面里套用一个母版页 在页面的的Poad_Load方法中 if(!IsPostBack){//绑定下拉框()}
在按钮的点击事件中
protected void BtnAdd_Click(object sender, EventArgs e)
{
string userId = DDLParent.SelectedItem.Value.ToString();
Response.Write(userId.ToString());
}
userId取到是null值
报错未将对象引用实例跟踪代码时 DDLParent.SelectedItem.Value值是null 问一下 怎么能取到下拉框中选择的值
在按钮的点击事件中
protected void BtnAdd_Click(object sender, EventArgs e)
{
string userId = DDLParent.SelectedItem.Value.ToString();
Response.Write(userId.ToString());
}
userId取到是null值
报错未将对象引用实例跟踪代码时 DDLParent.SelectedItem.Value值是null 问一下 怎么能取到下拉框中选择的值
问题是你绑定的时候绑的有值没?
DDLParent.DataValueField = "id"; //你应该是这一句没有写上去吧
DDLParent.DataTextField = "name";
DDLParent.DataBind();
{
string strSelParent = "select xxx from xxx where ROLE_PARENTID =0";
DataTable dtcm6008 = SystemUserDa.GetDataInfo(strSelParent);
if (dtcm6008.Rows.Count > 0)
{
ListItem item = new ListItem("请选择", "0");
DDLParent.Items.Add(item);
for (int i = 0; i < dtcm6008.Rows.Count; i++)
{
item = new ListItem(dtcm6008.Rows[i]["ROLE_NAME"].ToString(), dtcm6008.Rows[i]["SEQ_CM6008"].ToString());
DDLParent.Items.Add(item);
}
}
else
{
ListItem item = new ListItem("请选择", "0");
DDLParent.Items.Add(item);
}
}
<option value="0">请选择</option>
<option value="88">飞行员领导1</option>
<option value="90">飞行员2</option>
<option value="89">飞行员1</option>
</select>
//这样肯定取不到值,string userId = DDLParent.SelectedItem.Value.ToString();//这样才可以的
这样写也试过 DDLParent value text index都是空目前只有把这个页面引用的模板页 给去掉 才能获得值 暂时没有别的办法
如果有哪位高手能有办法 请继续留言我会关注的
为什么取不了值?
貌似也不行
Response.Write(userId.ToString());
}
如果不是 你点击按钮回发页面 首先进入Page_Load 里面 再到click事件里面
没写在 !IspostBack 的话 相当于又重新绑定了 你在click 里面DDLParent.SelectedValue 肯定就报未将对象引用到实例了
dropdownlist绑定值得问题,看看值绑定上没有,只要dropdownlist绑定上值了,这样取Value一点问题都没有
DDLParent.DataSource = dt;//dt是datetable 你懂的
DDLParent.DataTextField = "name";
DDLParent.DataValueField = "id";
DDLParent.DataBind();
DDLParent.Items.Insert(0, new ListItem("请选择", "0"));
获取方式
string userId=DDLParent.SelectedValue
还不行的话找微软吧