在 GridView1_RowDataBound 中找不到控件

cannot find controls in GridView1_RowDataBound

我在编辑 I 运行 GridView1_RowDataBound 时有一个网格视图,我需要在其中检索编辑模板中下拉列表的值以及当用户更改值时 I post 返回并填写以下下拉列表,但我得到的是空值,在我不确定哪里出了问题之前这段代码工作正常请帮忙:

aspx 代码

在编辑模板中有下拉列表的网格视图模板字段

<asp:TemplateField  HeaderText="Line">
  <HeaderTemplate>
    <asp:LinkButton ID="linenormal" runat="server" OnClick="LineNoraml_Click" AutoPostBack="true" ForeColor="White" Font-Underline="True" OnClientClick="populategridview">Line</asp:LinkButton>
                                     &nbsp;&nbsp;<br />
                                     <asp:ImageButton ID="lineasc" runat="server" Height="21px" ImageUrl="~/Images/uparrow.png" OnClick="LineAsc_Click" Width="23px" />
                   <asp:DropDownList ID="LineValues" runat="server" AutoPostBack="True" OnSelectedIndexChanged="LineValues_SelectedIndexChanged"></asp:DropDownList>
                                     <asp:ImageButton ID="linedesc" runat="server" Height="19px" ImageUrl="~/Images/downarrow.png" OnClick="LineDesc_Click" style="margin-top: 0px" Width="26px" />
                                     <br />
                                     <asp:TextBox ID="TextBox11" runat="server" Width="75px"></asp:TextBox>
                                     &nbsp;<asp:Button ID="LineFilter" runat="server" Text="Filter" OnClick="LineFilter_Click" />
               </HeaderTemplate>
                              <ItemTemplate>
                                 <asp:Label ID="line" runat="server" Text = '<%# Eval("[Line]") %>'></asp:Label>
                              </ItemTemplate>
                              <EditItemTemplate>
                                   <asp:DropDownList ID = "line1" runat="server"  AutoPostBack="True" OnSelectedIndexChanged="LineValues_SelectedIndexChanged"></asp:DropDownList>
                              </EditItemTemplate>
                              <FooterTemplate>
                                   <asp:TextBox ID = "linefooter" runat ="server" />
                              </FooterTemplate>
                          </asp:TemplateField>
            <asp:TemplateField  HeaderText="Machine">
                                 <HeaderTemplate>
                    <asp:LinkButton ID="machinenormal" runat="server" OnClick="MachineNormal_Click" AutoPostBack="true" ForeColor="White" Font-Underline="True">Machine</asp:LinkButton>
                                     &nbsp;&nbsp;<br />
                                     <asp:ImageButton ID="macgineasc" runat="server" Height="21px" ImageUrl="~/Images/uparrow.png" OnClick="MachineASC_Click" Width="23px" />
                   <asp:DropDownlist ID="MachineValues" runat="server" OnSelectedIndexChanged="MachineValues_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownlist>
                                      <asp:ImageButton ID="machinedesc" runat="server" Height="19px" ImageUrl="~/Images/downarrow.png" OnClick="MachineDesc_Click" style="margin-top: 0px" Width="26px" />
                                     <br />
                                     <asp:TextBox ID="TextBox12" runat="server" Width="82px"></asp:TextBox>
                                     &nbsp;<asp:Button ID="MachineFilter" runat="server" Text="Filter" OnClick="MachineFilter_Click" />
               </HeaderTemplate>
                              <ItemTemplate>
                                 <asp:Label ID="Machine" runat="server" Text = '<%# Eval("[Machine]") %>'></asp:Label>
                              </ItemTemplate>
                              <EditItemTemplate>
                                   <asp:DropDownList ID = "Machine1" runat ="server" />
                              </EditItemTemplate>
                              <FooterTemplate>
                                   <asp:TextBox ID = "Machine" runat ="server" />
                              </FooterTemplate>
                          </asp:TemplateField>
         <asp:TemplateField  HeaderText="Bag Type">
                                 <HeaderTemplate>
                                     Bag Type &nbsp;<br />
                   <asp:DropDownlist ID="BagTypeValues" runat="server"></asp:DropDownlist>
                                     <br />
                                     <asp:TextBox ID="TextBox13" runat="server" Width="98px"></asp:TextBox>
                                     &nbsp;<asp:Button ID="BagtypeFilter" runat="server" Text="Filter" OnClick="BagtypeFilter_Click" />
               </HeaderTemplate>
                              <ItemTemplate>
                                 <asp:Label ID="BagType" runat="server" Text = '<%# Eval("[Bag Type]") %>'></asp:Label>
                              </ItemTemplate>
                              <EditItemTemplate>
                                   <asp:DropDownList ID = "BagType1"  runat ="server" />
                              </EditItemTemplate>
                              <FooterTemplate>
                                   <asp:TextBox ID = "BagType" runat ="server" />
                              </FooterTemplate>
                          </asp:TemplateField>
         <asp:TemplateField  HeaderText="Prod Unit">
                                 <HeaderTemplate>
                                     Prod. Unit
            <br />
                   <asp:DropDownlist ID="ProdTypeValues" runat="server"></asp:DropDownlist>
               </HeaderTemplate>
                              <ItemTemplate>
                                 <asp:Label ID="ProdType1" runat="server" Text = '<%# Eval("[Prod# Unit]") %>'></asp:Label>
                              </ItemTemplate>
                              <EditItemTemplate>
                                   <asp:DropDownlist ID = "ProdType"  runat ="server" />
                              </EditItemTemplate>
                              <FooterTemplate>
                                   <asp:TextBox ID = "ProdType" runat ="server" />
                              </FooterTemplate>
                          </asp:TemplateField>

后面的代码

   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex && e.Row.RowState == DataControlRowState.Edit)
            {
               DropDownList ddlline = (DropDownList)e.Row.FindControl("line1");
                DropDownList ddlmachine = (DropDownList)e.Row.FindControl("Machine1");
                DropDownList ddlbagtype = (DropDownList)e.Row.FindControl("BagType1");
                DropDownList ddlprodunit = (DropDownList)e.Row.FindControl("ProdType");
                DropDownList ddlspeedunit = (DropDownList)e.Row.FindControl("SpeedTypeValues1");

                Label ddlln = (Label)e.Row.FindControl("line");
                Label ddlmac1 = (Label)e.Row.FindControl("Machine");
                Label ddlbt1 = (Label)e.Row.FindControl("BagType");
                Label ddlpu1 = (Label)e.Row.FindControl("ProdType1");
                Label ddlsu1 = (Label)e.Row.FindControl("SpeedTypeValues2");
**all of  the above return null which screws up my sql query below**
                DataTable dt = new DataTable();
                string sql = "Select Line FROM [WC Info] Where Plant = " + "\'" + PlantVal + "\'";
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                da.Fill(dt);
                ddlline.DataSource = dt;
                ddlline.DataTextField = "Line";
                ddlline.DataValueField = "Line";
                ddlline.DataBind();
                ddlline.SelectedValue = ddlln.Text;
                DataTable dt1 = new DataTable();
                string sql1 = "Select Distinct [Machine] FROM [WC Info] WHERE Line = '" + ddlln.Text + "'";
                SqlDataAdapter da1 = new SqlDataAdapter(sql1, con);
                da1.Fill(dt1);
                ddlmachine.DataSource = dt1;
                ddlmachine.DataTextField = "Machine";
                ddlmachine.DataValueField = "Machine";
                ddlmachine.DataBind();
                ddlmachine.SelectedValue = ddlmac1.Text;
                DataTable dt2 = new DataTable();
                string sql2 = "Select [Bag Type] FROM [Bag Type]";
                SqlDataAdapter da2 = new SqlDataAdapter(sql2, con);
                da2.Fill(dt2);
                ddlbagtype.DataSource = dt2;
                ddlbagtype.DataTextField = "Bag Type";
                ddlbagtype.DataValueField = "Bag Type";
                ddlbagtype.DataBind();
                ddlbagtype.SelectedValue = ddlbt1.Text;
                DataTable dt3 = new DataTable();
                string sql3 = "SELECT [Std Prod# Unit] FROM [WC Info] WHERE Line =" + "\'" + ddlln.Text + "\'" + "and [Machine] =" + "\'" + ddlmac1.Text + "\'";
                SqlDataAdapter da3 = new SqlDataAdapter(sql3, con);
                da2.Fill(dt3);
                ddlprodunit.DataSource = dt3;
                ddlprodunit.DataTextField = "Prod Unit Type";
                ddlprodunit.DataValueField = "Prod Unit Type";
                ddlprodunit.DataBind();
                ddlprodunit.SelectedValue = ddlpu1.Text;
                DataTable dt4 = new DataTable();
                string sql4 = "SELECT [Std Speed Unit] FROM [WC Info] WHERE Line =" + "\'" + ddlln.Text + "\'" + "and [Machine] =" + "\'" + ddlmac1.Text + "\'";
                SqlDataAdapter da4 = new SqlDataAdapter(sql4, con);
                da2.Fill(dt4);
                ddlspeedunit.DataSource = dt4;
                ddlspeedunit.DataTextField = "Std Speed Unit";
                ddlspeedunit.DataValueField = "Std Speed Unit";
                ddlspeedunit.DataBind();
                    ddlspeedunit.SelectedValue = ddlsu1.Text;
}
}

A运行 这是你代码的结果图片

image 请帮助我,因为我无法弄清楚我做错了什么。

提前致谢

这里是如何在 GridView RowDataBound 期间找到控件的方法

//for LinkButton
 System.Web.UI.WebControls.LinkButton lnk = ((System.Web.UI.WebControls.LinkButton)e.Row.Cells[0].FindControl("linkbutton id"));
 //For label
 System.Web.UI.WebControls.Label lbl = ((System.Web.UI.WebControls.Label)e.Row.Cells[0].FindControl("label ID"));
//For DropDownList
 System.Web.UI.WebControls.DropDownList ddl = ((System.Web.UI.WebControls.DropDownList)e.Row.Cells[0].FindControl("DropDownID"));
 //For TextBox
 System.Web.UI.WebControls.TextBox txt = ((System.Web.UI.WebControls.TextBox)e.Row.Cells[0].FindControl("textboxID"));
 This below code helped me find the control and also make the dropdownlist be populated with current values.


  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)
        {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
        DropDownList ddlline = (DropDownList)e.Row.FindControl("line1");
        DataTable dt = new DataTable();
        string sql = "Select Distinct Line FROM [WC Info] Where Plant = " + "\'" + PlantVal + "\'";
        SqlDataAdapter da = new SqlDataAdapter(sql, con);
        da.Fill(dt);
        ddlline.DataSource = dt;
        ddlline.DataTextField = "Line";
        ddlline.DataValueField = "Line";
        ddlline.DataBind();
        DataRowView dr = e.Row.DataItem as DataRowView;
        ddlline.SelectedValue = dr["Line"].ToString();
        DropDownList ddlmachine = (DropDownList)e.Row.FindControl("Machine1");
        DataTable dt1 = new DataTable();
        string sql1 = "Select Distinct [Machine] FROM [WC Info] WHERE Line = '" + ddlline.SelectedValue + "'";
        SqlDataAdapter da1 = new SqlDataAdapter(sql1, con);
        da1.Fill(dt1);
        ddlmachine.DataSource = dt1;
        ddlmachine.DataTextField = "Machine";
        ddlmachine.DataValueField = "Machine";
        ddlmachine.DataBind();
        ddlmachine.SelectedValue = dr["Machine"].ToString();
        DropDownList ddlbagtype = (DropDownList)e.Row.FindControl("BagType1");
        DataTable dt2 = new DataTable();
        string sql2 = "Select [Bag Type] FROM [Bag Type]";
        SqlDataAdapter da2 = new SqlDataAdapter(sql2, con);
        da2.Fill(dt2);
        ddlbagtype.DataSource = dt2;
        ddlbagtype.DataTextField = "Bag Type";
        ddlbagtype.DataValueField = "Bag Type";
        ddlbagtype.DataBind();
        ddlbagtype.SelectedValue = dr["Bag Type"].ToString();
        DropDownList ddlprodunit = (DropDownList)e.Row.FindControl("ProdType");
        DataTable dt3 = new DataTable();
        string sql3 = "SELECT [Std Prod# Unit] AS ProdType FROM [WC Info] WHERE Line =" + "\'" + ddlline.SelectedValue + "\'" + "and [Machine] =" + "\'" + ddlmachine.SelectedValue + "\'";
        SqlDataAdapter da3 = new SqlDataAdapter(sql3, con);
        da3.Fill(dt3);
        ddlprodunit.DataSource = dt3;
        ddlprodunit.DataTextField = "ProdType";
        ddlprodunit.DataValueField = "ProdType";
        ddlprodunit.DataBind();
        ddlprodunit.SelectedValue = dr["Prod# Unit"].ToString();
        DropDownList ddlspeedunit = (DropDownList)e.Row.FindControl("SpeedTypeValues1");
        DataTable dt4 = new DataTable();
        string sql4 = "SELECT [Std Speed Unit] FROM [WC Info] WHERE Line =" + "\'" + ddlline.SelectedValue + "\'" + "and [Machine] =" + "\'" + ddlmachine.SelectedValue + "\'";
        SqlDataAdapter da4 = new SqlDataAdapter(sql4, con);
        da4.Fill(dt4);
        ddlspeedunit.DataSource = dt4;
        ddlspeedunit.DataTextField = "Std Speed Unit";
        ddlspeedunit.DataValueField = "Std Speed Unit";
        ddlspeedunit.DataBind();
        ddlspeedunit.SelectedValue = dr["Speed Unit"].ToString();
        }
       }
     }