[c#]对于下拉列表页脚在网格中查看 (举出)

标签: Asp.net HTML C#
发布时间: 2017/2/25 3:23:05
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我在页脚在网格视图中有一个下拉列表。我想要它,因为这是为插入从数据库中读取。

我接到一个代码为此目的在这里,非常,但当就为页脚同样写了它给了我的错误︰ System.NullReferenceException。

我的代码是贝娄︰

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound" ShowFooter="True">
    <Columns>
        <asp:BoundField HeaderText="EId" DataField="EId" />
        <asp:TemplateField HeaderText = "CName">
            <ItemTemplate>
                <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("CName") %>' Visible = "false" />
                <asp:DropDownList ID="ddlCountries" runat="server">
                </asp:DropDownList>

            </ItemTemplate>
             <FooterTemplate>

              <asp:DropDownList ID="ddlCName"  CssClass="auto-style16" Width="80px" runat="server" ></asp:DropDownList>


        </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

在后面的代码︰

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = GetData("select * from TbExam inner join TBCourse on TBCourse.CId = TbExam.CId inner join TbCourseMajor on TbCourseMajor.CId=TBCourse.CId");
            GridView1.DataBind();
        }
    }

    private DataSet GetData(string query)
    {
        string conString = ConfigurationManager.ConnectionStrings["RigesterConnectionString1"].ConnectionString;

    SqlCommand cmd = new SqlCommand(query);
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    sda.Fill(ds);
                    return ds;
                }
            }
        }
    }

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Find the DropDownList in the Row
            DropDownList ddlCountries = (e.Row.FindControl("ddlCountries") as DropDownList);
            ddlCountries.DataSource = GetData("SELECT * FROM TBCourse  INNER JOIN TbCourseMajor ON  TBCourse.CId = TbCourseMajor.CId ");
            ddlCountries.DataTextField = "CName";
            ddlCountries.DataValueField = "CId";
            ddlCountries.DataBind();

            //Add Default Item in the DropDownList
            ddlCountries.Items.Insert(0, new ListItem("Please select"));

            DropDownList ddlCName = GridView1.FooterRow.FindControl("ddlCName") as DropDownList;
            ddlCName.DataSource = GetData("SELECT * FROM TBCourse  INNER JOIN TbCourseMajor ON  TBCourse.CId = TbCourseMajor.CId ");
            ddlCName.DataTextField = "CName";
            ddlCName.DataValueField = "CId";
            ddlCName.DataBind();

            //Add Default Item in the DropDownList
            ddlCName.Items.Insert(0, new ListItem("Please select"));

        }
    }

解决方法 1:

因为 OnRowDataBound 在 asp.net GridView 火灾后网格和页脚行中的每个数据绑定行可能不会捆住...所以 System.NullReferenceException. 是自然...所以您可以执行以下操作。

RowDataBound 后找到 GridView 中的页脚

和更新数据库

如此完整的代码

.aspx

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            ShowFooter="True" ondatabound="GridView1_DataBound">
    <Columns>
      <asp:BoundField HeaderText="EId" DataField="UserId" />
        <asp:TemplateField HeaderText = "CName">
            <ItemTemplate>
                <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("UserId") %>' Visible = "True" />

            </ItemTemplate>
             <FooterTemplate>

              <asp:DropDownList ID="ddlCName"  CssClass="auto-style16" Width="80px" runat="server" >
                  <asp:ListItem Selected="True">1</asp:ListItem>
                  <asp:ListItem>2</asp:ListItem>
                 </asp:DropDownList>


        </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

和.cs

protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = DbUtilities.GetDataTableFromSqlQuery("select top 10 * from Users");
        GridView1.DataBind();
    }

    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        GridViewRow row = GridView1.FooterRow;

        var firstName = ((DropDownList)row.Cells[1].FindControl("ddlCName")).SelectedValue;
        var s = firstName;
    }
赞助商