【问题标题】:Cannot Access DropdownList in EditItemTemplate inside Gridview无法访问 Gridview 内 EditItemTemplate 中的 DropdownList
【发布时间】:2013-06-03 11:06:31
【问题描述】:

我的 TT.aspx:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AutoGenerateEditButton="true" OnRowEditing="GridView1_RowEditing" >
    <Columns>
        <asp:TemplateField>
        <ItemTemplate>
            <asp:Literal ID="lit1" Text='<%#Eval("E_Name")%>' runat="server">

            </asp:Literal>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:DropDownList ID="Eq" AppendDataBoundItems="true" runat="server" DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="Name">

            </asp:DropDownList>

        </EditItemTemplate>

        </asp:TemplateField>

    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [E_Name], [Problem], [Solution] FROM [Equipment] WHERE ([O_ID] = @O_ID)">
    <SelectParameters>
        <asp:QueryStringParameter Name="O_ID" QueryStringField="TT" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [Name] FROM [Parts]">
</asp:SqlDataSource>

我的 TT.aspx.cs

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Literal xx = (Literal)GridView1.Rows[e.NewEditIndex].FindControl("lit1");
        String x =  xx.Text;
        DropDownList x1 = (DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("Eq");
        x1.SelectedValue = x;
    }

我是 asp.net 的新手,我只使用此代码进行测试。我遇到的问题是第二个FindControl("Eq") 总是返回null,而第一个(对于文字)返回正确的值。我尝试将 gridview 的编辑索引设置为 e.NewEditIndex 但似乎不起作用。

基本上我想要发生的是,如果用户单击一行上的编辑,则在下拉列表中选择标签中的原始数据绑定值。

有人可以指导我吗?

【问题讨论】:

  • 在询问之前我已经检查过了,但我不明白它是如何关联的。我这样说是因为他在RowDataBound 方法中实现代码,而我在RowEditing 方法中实现它。我也已经尝试过GridView1.EditIndex = e.NewEditIndex;,但它没有用。如果我错了,请纠正我。

标签: asp.net gridview drop-down-menu itemtemplate edititemtemplate


【解决方案1】:

RowEditing 事件只是给出正在编辑的行索引。您需要将编辑行索引设置为网格并重新绑定它。如果您需要在EditItemTemplate 中设置值或绑定控件,则必须在RowDateBound 事件中进行,因为您将在RowEditing 事件中重新绑定网格,因此RowDataBound 事件也将给你EditItemTemplate的控件。

this answer所示

protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
  // grid view's edit index has been changed so rebind it
  gv.EditIndex = e.NewEditIndex;
}

protected void gv_RowDataBound(object sender, GridViewEditEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
  {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
          ddList.SelectedValue = "set your value here";
        }
   }
}

【讨论】:

  • +1 但如果您使用 DataSource 属性和 DataBind 方法手动绑定 GridView,则只需设置 EditIndex。这个答案将在没有 RowEditing 事件重新绑定 GridView 的情况下工作。
  • 我现在就试试,但是快速提问;单击编辑按钮会调用RowDataBound 方法吗?如果没有,那么我如何通过RowEditing 方法以编程方式调用它?
  • 按下编辑按钮将调用RowEditing 事件,并且在重新绑定网格后的行编辑事件中,RowDateBound 事件将触发,正如@Sean 指出的那样,您正在使用DataSource 属性,因此您不会需要重新绑定刚刚设置的网格EditIndexRowDataBound会自动触发。
  • 现在Literal xx = (Literal)e.Row.FindControl("lit1"); 返回空值。这是从RowDataBound 方法中引用“lit1”的正确方法吗?
  • 我管理了一个变通的解决方案,有点混乱,但可以完成工作。我将标签内的文本保存在全局变量中,一旦我在RowEditing 方法中读取它,然后在RowDataBound 方法中读取它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-21
  • 1970-01-01
  • 1970-01-01
  • 2016-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多