我发生了一些奇怪的事情,也许我不知道什么?
我正在尝试填充下拉列表(在editItemTemplate中),并且当网格视图加载填充列时,使用字符串而不是现在包含的id。
ASPX(我遇到的问题是&
<asp:GridView ID="gvAdminArticleAdd".....
<asp:TemplateField HeaderText="invsId" SortExpression="invsId">
<EditItemTemplate>
<asp:DropDownList ID="ddl_invNames" runat="server" AutoPostBack="True" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl_insLabel" runat="server" Text='<%# Bind("invsId") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddl_invNamesNew" runat="server" AutoPostBack="True" />
</FooterTemplate>
</asp:TemplateField>
代码隐藏
protected void gvAdminArticleAdd_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//111111
//finding cotrols into the edit rows event
if (e.Row.RowState == DataControlRowState.Edit)
{
DropDownList ddlImages = (DropDownList)e.Row.FindControl("ddlImages");
ddlImages.DataSource = GetPdfs();
ddlImages.DataBind();
DropDownList ddlinvsNames = (DropDownList)e.Row.FindControl("ddl_invNames");
ArrayList invList = GetInvestigatorNames();
ddlinvsNames.DataSource = invList;
ddlinvsNames.DataBind();
}
//222222
//finding cotrols into rows
Label insLabel = (Label)e.Row.FindControl("lbl_insLabel");
int invsLabelId = int.Parse(insLabel.Text);
insLabel.Text = connection.GetInvsNameById(invsLabelId);
}
}
我在RowDataBound事件中出现的问题,我无法弄清楚它出了什么问题
// 111111和// 22222正常工作(如果我评论其中一个),但不能在一起。怎么会这样?
如果我把它们放在一起我在这行代码上出错了
int invsLabelId = int.Parse(insLabel.Text);
对象引用未设置为对象的实例。 所以错误在上一行,但我无法理解为什么。
答案 0 :(得分:0)
// 111111和// 22222正常工作(如果我评论其中一个), 但不是在一起。怎么会这样?
你不需要那两个人一起工作。在给定时间只有一个可用。
if (e.Row.RowState == DataControlRowState.Edit)
{
// EditItemTemplate - only controls inside EditItemTemplate are available here.
var ddlImages = (DropDownList)e.Row.FindControl("ddlImages");
var ddlinvsNames = (DropDownList)e.Row.FindControl("ddl_invNames");
}
else
{
// ItemTemplate - only controls inside ItemTemplate are available here.
var insLabel = (Label)e.Row.FindControl("lbl_insLabel");
}
答案 1 :(得分:0)
问题是您的dropDownList仅在EditMode期间添加。这是您在if(e.Row.RowType == DataControlRowType.DataRow)代码块中检索的。没关系,您将能够正确检索下拉列表。但是在相同的代码块中,您正在检索在编辑模式下无法使用的Label ddl_invNames,因为您已在ItemTemplate中添加了此标签。所以错误必须在这个地方。但是,如果你对整个块进行评论,那么代码会直接转到/ 222222,在那里你试图访问标签,在行的编辑模式中它根本就不存在