C# 在行数据绑定事件上找不到隐藏的输入控件
这是我的aspx文件内容C# 在行数据绑定事件上找不到隐藏的输入控件,c#,gridview,webforms,C#,Gridview,Webforms,这是我的aspx文件内容 这是.cs端 private LMSDataAccess.LookupsDataContext LkpDC = new LMSDataAccess.LookupsDataContext(ConfigFile.DBConnStr); private System.Collections.Generic.List<LMSDataAccess.GeneralSetting> GeneralSettingList = null; protected v
这是.cs端
private LMSDataAccess.LookupsDataContext LkpDC = new LMSDataAccess.LookupsDataContext(ConfigFile.DBConnStr);
private System.Collections.Generic.List<LMSDataAccess.GeneralSetting> GeneralSettingList = null;
protected void Page_Load(object sender, EventArgs e)
{
base.ModuleID = 27;
if (!IsPostBack)
{
if (Request["AID"] != null)
hdnAID.Value = Request["AID"];
BindData();
}
}
private void BindData()
{
GeneralSettingList = LkpDC.GeneralSettingsGetAll(Common.NVLInt(hdnAID.Value),null).ToList();
GridView1.DataSource = GeneralSettingList;
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtUpdateGeneralSettingName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtUpdateGeneralSettingName");
TextBox txtUpdateGeneralSettingValue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtUpdateGeneralSettingValue");
var hdnCompany = (HiddenField)GridView1.Rows[e.RowIndex].FindControl("hdnCompanyId") as HiddenField;
string KeyValue = GridView1.DataKeys[e.RowIndex].Value.ToString();
LkpDC.GeneralSettingsUpdate(Convert.ToInt32(KeyValue),
txtUpdateGeneralSettingName.Text,
txtUpdateGeneralSettingValue.Text,
Convert.ToInt32(hdnCompany.Value));
GridView1.EditIndex = -1;
BindData();
ResultCode = 0;
}
protected List<LMSDataAccess.Company> GetAllCompanies()
{
System.Collections.Generic.List<LMSDataAccess.Company> CompanyList = null;
LMSDataAccess.OrganizationDataContext OrgDC = new LMSDataAccess.OrganizationDataContext(ConfigFile.DBConnStr);
CompanyList = OrgDC.CompaniesGetAll(null).ToList();
return CompanyList;
}
protected void drpCompanyList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.Parent.Parent;
var hdnCompany = row.FindControl("hdnCompanyId") as HiddenField;
hdnCompany.Value = ddl.SelectedValue;
}
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var ddl = e.Row.FindControl("drpCompanyList") as DropDownList;
//here is the problem.
var hdn = e.Row.FindControl("hdnCompanyId") as HiddenField;
if (ddl != null)
{
ddl.DataSource = GetAllCompanies();
ddl.DataValueField = "CompanyID";
ddl.DataTextField = "CompanyName";
ddl.DataBind();
}
}
}
private LMSDataAccess.LookupsDataContext LkpDC=new LMSDataAccess.LookupsDataContext(ConfigFile.DBConnStr);
private System.Collections.Generic.List GeneralSettingList=null;
受保护的无效页面加载(对象发送方、事件参数e)
{
base.ModuleID=27;
如果(!IsPostBack)
{
如果(请求[“援助”]!=null)
hdnAID.Value=Request[“AID”];
BindData();
}
}
私有void BindData()
{
GeneralSettingList=LkpDC.GeneralSettingsGetAll(Common.NVLInt(hdnAID.Value),null.ToList();
GridView1.DataSource=通用设置列表;
GridView1.DataBind();
}
受保护的无效GridView1\u行取消编辑(对象发件人,GridViewCancelEditEventArgs e)
{
GridView1.EditIndex=-1;
BindData();
}
受保护的无效GridView1_行编辑(对象发送方,GridViewEditEventArgs e)
{
GridView1.EditIndex=e.NewEditIndex;
BindData();
}
受保护的无效GridView1_行更新(对象发送方,GridViewUpdateEventArgs e)
{
TextBox txtUpdateGeneralSettingName=(TextBox)GridView1.Rows[e.RowIndex].FindControl(“txtUpdateGeneralSettingName”);
TextBox txtUpdateGeneralSettingValue=(TextBox)GridView1.Rows[e.RowIndex].FindControl(“txtUpdateGeneralSettingValue”);
var hdnCompany=(HiddenField)GridView1.Rows[e.RowIndex].FindControl(“hdnCompanyId”)作为HiddenField;
string KeyValue=GridView1.DataKeys[e.RowIndex].Value.ToString();
LkpDC.GeneralSettingsUpdate(转换为32(键值),
txtUpdateGeneralSettingName.Text,
txtUpdateGeneralSettingValue.Text,
转换为32(hdnCompany.Value));
GridView1.EditIndex=-1;
BindData();
结果代码=0;
}
受保护列表GetAllCompanys()
{
System.Collections.Generic.List CompanyList=null;
LMSDataAccess.OrganizationDataContext OrgDC=新的LMSDataAccess.OrganizationDataContext(ConfigFile.DBConnStr);
CompanyList=OrgDC.companyGetAll(null.ToList();
返回公司列表;
}
受保护的无效drpCompanyList\u SelectedIndexChanged(对象发送方,事件参数e)
{
DropDownList ddl=(DropDownList)发送方;
GridViewRow=(GridViewRow)ddl.Parent.Parent;
var hdnCompany=row.FindControl(“hdnCompanyId”)作为HiddenField;
hdnCompany.Value=ddl.SelectedValue;
}
受保护的void GridView1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
var ddl=e.Row.FindControl(“drpCompanyList”)作为DropDownList;
//问题就在这里。
var hdn=e.Row.FindControl(“hdnCompanyId”)作为HiddenField;
如果(ddl!=null)
{
ddl.DataSource=getAllCompanys();
ddl.DataValueField=“CompanyID”;
ddl.DataTextField=“CompanyName”;
ddl.DataBind();
}
}
}
我最喜欢的问题是,当GridView1\u RowDataBound和drpCompanyList\u选择DexChangedFeed时,无法获取hdnCompanyId元素。我总是得到null。(如注释行所示)
我在GridView1_RowDataBound事件方法下尝试过,比如e.Row.FindControl(“hdnCompanyId”)
但它没有起作用。
我的最终目标是设置并获取此元素以控制dropdownlist中的选定项
我认为代码写得很好,但我想我缺少了一些关于用户控件元素层次结构的东西。
你能帮我解释一下我遗漏了什么吗?我发现问题在于选择了错误的输入类型。它必须是
HtmlInputHidden
,而不是HiddenField
。我希望有人能从这个问题中获得有用的信息
private LMSDataAccess.LookupsDataContext LkpDC = new LMSDataAccess.LookupsDataContext(ConfigFile.DBConnStr);
private System.Collections.Generic.List<LMSDataAccess.GeneralSetting> GeneralSettingList = null;
protected void Page_Load(object sender, EventArgs e)
{
base.ModuleID = 27;
if (!IsPostBack)
{
if (Request["AID"] != null)
hdnAID.Value = Request["AID"];
BindData();
}
}
private void BindData()
{
GeneralSettingList = LkpDC.GeneralSettingsGetAll(Common.NVLInt(hdnAID.Value),null).ToList();
GridView1.DataSource = GeneralSettingList;
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtUpdateGeneralSettingName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtUpdateGeneralSettingName");
TextBox txtUpdateGeneralSettingValue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtUpdateGeneralSettingValue");
var hdnCompany = (HiddenField)GridView1.Rows[e.RowIndex].FindControl("hdnCompanyId") as HiddenField;
string KeyValue = GridView1.DataKeys[e.RowIndex].Value.ToString();
LkpDC.GeneralSettingsUpdate(Convert.ToInt32(KeyValue),
txtUpdateGeneralSettingName.Text,
txtUpdateGeneralSettingValue.Text,
Convert.ToInt32(hdnCompany.Value));
GridView1.EditIndex = -1;
BindData();
ResultCode = 0;
}
protected List<LMSDataAccess.Company> GetAllCompanies()
{
System.Collections.Generic.List<LMSDataAccess.Company> CompanyList = null;
LMSDataAccess.OrganizationDataContext OrgDC = new LMSDataAccess.OrganizationDataContext(ConfigFile.DBConnStr);
CompanyList = OrgDC.CompaniesGetAll(null).ToList();
return CompanyList;
}
protected void drpCompanyList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.Parent.Parent;
var hdnCompany = row.FindControl("hdnCompanyId") as HiddenField;
hdnCompany.Value = ddl.SelectedValue;
}
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var ddl = e.Row.FindControl("drpCompanyList") as DropDownList;
//here is the problem.
var hdn = e.Row.FindControl("hdnCompanyId") as HiddenField;
if (ddl != null)
{
ddl.DataSource = GetAllCompanies();
ddl.DataValueField = "CompanyID";
ddl.DataTextField = "CompanyName";
ddl.DataBind();
}
}
}