我在网格视图中的页脚中有一个下拉列表。我希望它从数据库中读取,因为这是用于插入。
我从here获得了一个用于此目的的代码并且它可以正常工作,但是当我为页脚编写相同内容时它会给出错误: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"));
}
}
答案 0 :(得分:1)
由于asp.net GridView上的<charset-params>
<input-charset>
<resource-path>/*</resource-path>
<java-charset-name>UTF-8</java-charset-name>
</input-charset>
</charset-params>
在Grid和页脚行中的每个数据绑定行之后触发可能没有绑定...所以OnRowDataBound
很自然......所以你可以做以下事情。
在rowDataBound中找到GridView中的页脚
和更新数据库
如此完整的代码
的.aspx
System.NullReferenceException.
和.cs
<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>
答案 1 :(得分:1)
这是有效的:
protected void GridView1_DataBound1(object sender, EventArgs e)
{
//GridViewRow row = GridView1.FooterRow;
//var CName = ((DropDownList)row.Cells[1].FindControl("ddlCName")).SelectedValue;
//var s = CName;
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"));
}