有一个Formview,里面有一个TextBox控件。想在databinding事件中修改TextBox控件。但是网页报错说找不到该控件。代码如下:
protected void FormView1_DataBinding(object sender, EventArgs e)
{
TextBox TextBoxUserName = (TextBox)FormView1.Row.FindControl("TextBoxRealName"); }错误是:System.NullReferenceException: Object reference not set to an instance of an object.请前辈执教。先谢谢大家的宝贵时间了。
protected void FormView1_DataBinding(object sender, EventArgs e)
{
TextBox TextBoxUserName = (TextBox)FormView1.Row.FindControl("TextBoxRealName"); }错误是:System.NullReferenceException: Object reference not set to an instance of an object.请前辈执教。先谢谢大家的宝贵时间了。
解决方案 »
- asp.net不同目录下的文件怎么用相同的路径引用同一个css
- jquery源代码中 (function(){//代码})(); 写法是什么回事?
- 设置表格单元格的背景图片为拉伸,不要平铺
- 【新手问题】未解决的新手问题,最后的积分
- 垂直搜索,哪位有经验,小弟虚心求教
- WEB考试系统问题二:如何实时保存考生的答案?
- sql注入
- asp.net page.user 在什么时候能够获得 user的值呢?
- 如何在gridview 刷新后,让 SelectedRowStyle 的 BackColor 恢复到正常
- asp与asp.net间的session不能互相传递吗?
- 这样也能构成重载吗?
- 动态生成无限级联DropDownList,获取动态控件选择项的问题
TextBox TextBoxUserName = (TextBox)FormView1.FindControl("TextBoxRealName");就找到了
具体可以参考我这里给你的代码(这里是访问下拉框控件的,你改成textbox就是了):
protected void FormView1_ItemCreated(object sender, EventArgs e)
{
DropDownList test;
switch (FormView1.CurrentMode)
{
case FormViewMode.Edit:
test = ((DropDownList)FormView1.Row.FindControl("DropDownList3"));
test.Attributes.Add("onchange", "FControl('" + test.UniqueID + "');");
break;
case FormViewMode.Insert:
test = ((DropDownList)FormView1.Row.FindControl("DropDownList3"));
test.Attributes.Add("onchange", "FControl('" + test.UniqueID + "');");
break;
}
}
txBox=(TextBox)DataSourceItemView.FindControl("TextBox");
先找到这个控件。然后txBox.ClientID, 并且Javascript需要在Page_Load中加载
3:但是这里还有问题,方法2中只是适合Default View,如果View有了变化,例如原来是Readonly转换到EditTemplate的时候,EditTemplate里面的控件使用ItemView.Find无法找到。所以需要另外的方法。在Item_Created中加载Javascript
我这里实现的是弹出窗口,返回一个数值
protected void FormView1_ItemCreated(object sender, EventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.Edit)
{
TextBox CustTextBox = (TextBox)FormView1.FindControl("main_customerTextBox");
if (CustTextBox == null) return;
string js = "<script language='javascript'>";
js = js + "function findcust(CustButton)";
js = js + "{";
js = js + " var ttop = CustButton.offsetTop; ";
js = js + " var thei = CustButton.clientHeight;";
js = js + " var tleft = CustButton.offsetLeft;";
js = js + " while (CustButton = CustButton.offsetParent){ttop+=CustButton.offsetTop; tleft+=CustButton.offsetLeft;}"; js = js + " var returnValue=window.showModalDialog('../Common/CommonFindCustomer.aspx','Pass paramerter to Child Window','dialogWidth:600px;dialogHeight:500px;dialogLeft:'+tleft+';dialogTop:'+ttop+';resizable:yes ');";
js = js + " if (returnValue == null) return;"; js = js + " var CtrlCust=document.getElementById('" + CustTextBox.ClientID + "');";
js = js + " if (CtrlCust==null)";
js = js + " alert('Not Found Customer Control');";
js = js + " else";
js = js + " CtrlCust.value=returnValue;";
js = js + "}</script>"; ClientScript.RegisterStartupScript(this.GetType(),"FindCustScript", js);
}
这样可以带来一定好处:
1、代码比较好写(不用对应多个不同模版的不同id的同种控件写冗余代码);
2、ViewState(页面状态)可以平滑‘继承’(因为某些控件也许不需要数据绑定,这样FormView在不同模式下切换时,因为id一样,viewstate得以保留。当然,如果不同模版里的同一个id对应不同类型控件,结果可能就糟糕了,例如一个label和一个textbox的id一样)第二点有误,经过测试,ViewState在不同模式间切换后没有保留。应该是FormView在模式切换中有一个删除ViewState的过程。当然从效率角度上来讲也确实应该这样。