当我使用(DropDownList)e.Row.FindControl时,我变为null

时间:2011-05-26 15:45:57

标签: c# asp.net gridview

这是我的代码,它给出了ddl = null的值:

//event handler to bind the data to the row before rendering the gridview
protected void gridview1_RowDataBound(object sender ,GridViewRowEventArgs e)
{
    if ((e.Row.RowType == DataControlRowType.DataRow)&&(e.Row.DataItem!=null))
    {
        //DropDownList ddlNo = (DropDownList)e.Row.FindControl("ddlNo");
        DropDownList ddlNo = (DropDownList)e.Row.FindControl("ddlNo");

        if (ddlNo != null)
        {
            ddlNo.DataSource = Test.GetData();
            ddlNo.DataBind();
            ddlNo.SelectedValue = gridview1.DataKeys[e.Row.RowIndex].Values[0].ToString();//datakeys are used to retain the value of the no. in edit mode    
        }
    }
}

这是我的aspx代码:

<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false" AutoGenerateDeleteButton="false" AutoGenerateEditButton="false" ShowFooter="true" EmptyDataText="No data available." PagerStyle-HorizontalAlign="Right" AllowPaging="true" AllowSorting="true" GridLines="Vertical" Width="98%" EnableViewState="true" OnRowCancelingEdit="gridview1_RowCancelingEdit" OnRowDataBound="gridview1_RowDataBound" OnRowEditing="gridview1_RowEditing" OnRowUpdating="gridview1_RowUpdating" OnRowCommand="gridview1_RowCommand" OnRowDeleting="gridview1_RowDeleting" DataKeyNames="testID">
    <Columns>
        <asp:TemplateField HeaderText="Select" HeaderStyle-HorizontalAlign="Left">
            <ItemTemplate>
                <asp:Label ID="lblSelect" runat="server" Text="Select"></asp:Label>
                <%-- <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelect_CheckChanged" />--%>
            </ItemTemplate>
            <HeaderStyle HorizontalAlign="Left" />
        </asp:TemplateField>
        <asp:BoundField DataField="testID" HeaderText="SearchId" />
        <asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Left">
            <EditItemTemplate>
                <asp:TextBox ID="txtName" runat="server" Text='<%#Eval("testDesc") %>' />
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtNewName" runat="server"></asp:TextBox>
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="lblName" runat="server" Text='<%# Bind("testDesc") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle HorizontalAlign="Left" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="No." HeaderStyle-HorizontalAlign="Left">
            <EditItemTemplate>
                <asp:DropDownList ID="ddlNo" runat="server" DataTextField="testNo" DataValueField="testNo" />
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblNo" runat="server" Text='<%# Bind("testNo") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="ddlNewNo" runat="server" DataTextField="testNo" DataValueField="testNo">
                </asp:DropDownList>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Edit" ShowHeader="False">
            <EditItemTemplate>
                <asp:LinkButton ID="linkbt1" runat="server" CausesValidation="true" CommandName="Update" Text="Update">
                </asp:LinkButton>
                <asp:LinkButton ID="linkbt2" runat="server" CausesValidation="false" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:LinkButton ID="linkbt1" runat="server" CausesValidation="false" CommandName="Edit" Text="Edit">

                </asp:LinkButton>
            </ItemTemplate>
            <FooterTemplate>
                <asp:LinkButton ID="linkbt2" runat="server" CausesValidation="false" CommandName="AddNew" Text="AddNew">

                </asp:LinkButton>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:CommandField HeaderText="Delete" ShowDeleteButton="true" ShowHeader="true" />
    </Columns>
</asp:GridView>

任何人都可以为此提供相关解决方案吗?

2 个答案:

答案 0 :(得分:3)

你把ddl no放在footer template中,但你只是在(e.Row.RowType == DataControlRowType.DataRow)将其更改为FooterRow并且它应该找到它

将上述行更改为

if(e.Row.RowType == DataControlRowType.Footer&amp;&amp; e.Row.DataItem!= null)

答案 1 :(得分:0)

                                    &LT;% - 的DataKeyNames = “Environment_ID” - %GT;                                                                                                                                                                            '/&gt;                                                                                                                               &LT;% -                                                              “&GT;                                                                                                                             '/&gt;                                                                                                                                                                                   - %GT;                                                                                                                                                                                                                                         '/&gt;

                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="lblapp" runat="server" Text='<%# Eval("AppName") %>'></asp:Label>
                                        </ItemTemplate>
                                        <FooterTemplate>
                                            <asp:DropDownList ID="ddlAppFooter" class="SearchBoxRTD" runat="server" DataTextField="AppName"
                                                DataValueField="SNO">
                                            </asp:DropDownList>
                                        </FooterTemplate>
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="TestType" HeaderStyle-Width="50">
                                        <EditItemTemplate>
                                            <asp:DropDownList ID="ddlTestTypeDataRow" class="SearchBoxRTD" runat="server" DataTextField="Testingtypedescription"
                                                DataValueField="TestingTypeID">
                                            </asp:DropDownList>
                                            <asp:HiddenField ID="hdnTesttype" runat="server" Value='<%# Bind("Testtype") %>' />
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="lblTestType" runat="server" Text='<%# Eval("Testingtypedescription") %>'></asp:Label>
                                        </ItemTemplate>
                                        <FooterTemplate>
                                            <asp:DropDownList ID="ddlTestTypeFooter" class="SearchBoxRTD" runat="server" DataTextField="Testingtypedescription"
                                                DataValueField="TestingTypeID">
                                            </asp:DropDownList>
                                        </FooterTemplate>
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Scenario Type" HeaderStyle-Width="50">
                                        <EditItemTemplate>
                                            <asp:DropDownList ID="ddlScenarioTypeDataRow" class="SearchBoxRTD" runat="server" DataTextField="ScenarioTypedescription"
                                                DataValueField="ScenarioTypeID">
                                            </asp:DropDownList>
                                            <asp:HiddenField ID="hdnScenariotype" runat="server" Value='<%# Bind("ScenarioTypeID") %>' />
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="lblScenarioType" runat="server" Text='<%# Eval("ScenarioTypedescription") %>'></asp:Label>
                                        </ItemTemplate>
                                        <FooterTemplate>
                                            <asp:DropDownList ID="ddlScenarioTypeFooter" class="SearchBoxRTD" runat="server"
                                                DataTextField="ScenarioTypedescription" DataValueField="ScenarioTypeID">
                                            </asp:DropDownList>
                                        </FooterTemplate>
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Scenario">
                                        <EditItemTemplate>
                                            <asp:TextBox ID="txtScenario" runat="server" Text='<%# Bind("Scenario") %>'></asp:TextBox>
                                            <asp:RequiredFieldValidator ID="RFDGEdUI" runat="server" Font-Bold="true" ErrorMessage="*"
                                                ControlToValidate="txtScenario" ValidationGroup="EditScenario"></asp:RequiredFieldValidator>
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="LblScenario" runat="server" Text='<%# Bind("Scenario") %>' />
                                        </ItemTemplate>
                                        <FooterTemplate>
                                            <asp:TextBox ID="txtfootScenario" runat="server"></asp:TextBox>
                                            <asp:RequiredFieldValidator ID="RFDGScenario" runat="server" Font-Bold="true" ErrorMessage="*"
                                                ControlToValidate="txtfootScenario" ValidationGroup="AddScenario"></asp:RequiredFieldValidator>
                                        </FooterTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Status">
                                        <EditItemTemplate>
                                            <asp:CheckBox ID="chkScenariostatus" runat="server" Checked='<%# Bind("Status") %>' />
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="LblScenarioStatus" runat="server" Text='<%# Bind("Status") %>' />
                                        </ItemTemplate>
                                        <FooterTemplate>
                                            <asp:CheckBox ID="chkScenariofooter" runat="server" />
                                        </FooterTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Edit" ShowHeader="False">
                                        <EditItemTemplate>
                                            <asp:LinkButton ID="lbkUpdateScenario" runat="server" ValidationGroup="EditScenario"
                                                CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
                                            <asp:LinkButton ID="lnkCancelScenario" runat="server" CausesValidation="False" CommandName="Cancel"
                                                Text="Cancel"></asp:LinkButton>
                                        </EditItemTemplate>
                                        <FooterTemplate>
                                            <asp:LinkButton ID="lnkAddScenario" runat="server" ValidationGroup="AddScenario"
                                                CausesValidation="True" CommandName="Insert" Text="Insert"></asp:LinkButton>
                                        </FooterTemplate>
                                        <ItemTemplate>
                                            <asp:LinkButton ID="lnkEditScenario" runat="server" CausesValidation="False" CommandName="Edit"
                                                Text="Edit"></asp:LinkButton>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:TemplateField>
                                </Columns>
                                <EmptyDataTemplate>
                                    No Applications available!
                                </EmptyDataTemplate>
                            </asp:GridView>

-------------------------------- CS -------------- ------------------------------------------

protected void grdScenario_RowDataBound(object sender,GridViewRowEventArgs e)         {             尝试             {                 DataSet ds = GetData(“qtp_getservice”);                 if(e.Row.RowType == DataControlRowType.DataRow)                 {

                //services,environment,platform
                // bool flag = false;


                if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
                {
                    Label lbl = (Label)e.Row.FindControl("LblScenarioStatus");
                    if (null != lbl && lbl.Text != "")
                    {
                        lbl.Text = (string.Compare(lbl.Text, "True", true) == 0) ? "Enabled" : "Disabled";
                    }
                }
              if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
               // {
                   // for (int i = 0; i < grdScenario.Rows.Count ; i++)
                   // {

               // foreach (GridViewRow gvRow in grdScenario.Rows)
                {

                    DropDownList ddlApp = (DropDownList)e.Row.FindControl("ddlAppDataRow");
                    DropDownList ddlTT = (DropDownList)e.Row.FindControl("ddlTestTypeDataRow");
                    DropDownList ddlST = (DropDownList)e.Row.FindControl("ddlScenarioTypeDataRow");
                    //Label lblapp = ((Label)e.Row.Cells[2].FindControl("lblapp"));
                    //Label lblTestType = ((Label)e.Row.Cells[3].FindControl("lblTestType"));
                    //Label lblScenarioType = ((Label)e.Row.Cells[4].FindControl("lblScenarioType"));

                    // Label lblapp = ((Label)grdScenario.Rows[i].Cells[2].FindControl("lblapp"));
                    //Label lblTestType = ((Label)grdScenario.Rows[i].Cells[3].FindControl("lblTestType"));
                    // Label lblScenarioType = ((Label)grdScenario.Rows[i].Cells[4].FindControl("lblScenarioType"));


                    if (ddlApp != null)
                    {
                        HiddenField hdnAppid = (HiddenField)e.Row.FindControl("hdnAppid");
                        ddlApp.DataSource = ds.Tables[4].DefaultView;
                        ddlApp.DataTextField = "AppName";
                        ddlApp.DataValueField = "SNO";
                        ddlApp.DataBind();
                        //ddlApp.SelectedIndex = ddlApp.Items.IndexOf(ddlApp.Items.FindByText(grdScenario.DataKeys[e.Row.RowIndex].Values[1].ToString()));
                        //ddlApp.SelectedItem.Text = lblapp.Text.ToString();
                        ddlApp.SelectedValue = hdnAppid.Value;

                    }
                    if (ddlTT != null)
                    {
                        HiddenField hdnTesttype = (HiddenField)e.Row.FindControl("hdnTesttype");
                        ddlTT.DataSource = ds.Tables[5].DefaultView;
                        ddlTT.DataTextField = "Testingtypedescription";
                        ddlTT.DataValueField = "TestingTypeID";
                        ddlTT.DataBind();
                        //ddlTT.SelectedItem.Text = lblTestType.Text.ToString();
                        ddlTT.SelectedValue = hdnTesttype.Value; 
                        //ddlTT.SelectedIndex = ddlTT.Items.IndexOf(ddlTT.Items.FindByText(grdservices.DataKeys[e.Row.RowIndex].Values[2].ToString()));
                    }
                    if (ddlST != null)
                    {
                        HiddenField hdnScenariotype = (HiddenField)e.Row.FindControl("hdnScenariotype");
                        ddlST.DataSource = ds.Tables[6].DefaultView;
                        ddlST.DataTextField = "ScenarioTypeDescription";
                        ddlST.DataValueField = "ScenarioTypeID";
                        ddlST.DataBind();
                        ddlST.SelectedValue = hdnScenariotype.Value;
                        //ddlST.SelectedItem.Text = lblScenarioType.Text.ToString();
                        //ddlST.SelectedIndex = ddlST.Items.IndexOf(ddlST.Items.FindByText(grdScenario.DataKeys[e.Row.RowIndex].Values[3].ToString()));
                    }
                }
                   // }
               //// }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                //Bind environment,platform at footer.
                DropDownList cmbNewType = (DropDownList)e.Row.FindControl("ddlAppFooter");
                cmbNewType.DataSource = ds.Tables[4].DefaultView;
                cmbNewType.DataBind();
                DropDownList cmbNewType1 = (DropDownList)e.Row.FindControl("ddlTestTypeFooter");
                cmbNewType1.DataSource = ds.Tables[5].DefaultView;
                cmbNewType1.DataBind();
                DropDownList cmbNewType2 = (DropDownList)e.Row.FindControl("ddlScenarioTypeFooter");
                cmbNewType2.DataSource = ds.Tables[6].DefaultView;
                cmbNewType2.DataBind();

            }
        }
        catch (System.Exception) { }
    }