我使用asp.net构建我的网站。 我试图将数据绑定到我的gridview中找到的下拉列表。 当我尝试将该数据连接到下拉列表时,我得到了无引用。 可能是什么问题?
ASPX
<asp:GridView ID="TauluGridcompetitors" runat="server">
<Columns>
<asp:TemplateField HeaderText="No Weapon">
<EditItemTemplate>
<asp:DropDownList ID="NoWeaponDDL" runat="server" AutoPostBack="true"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Short Weapon"></asp:TemplateField>
<asp:TemplateField HeaderText="Long Weapon"></asp:TemplateField>
<asp:TemplateField HeaderText="Dual Weapon"></asp:TemplateField>
<asp:TemplateField HeaderText="Group Weapon"></asp:TemplateField>
</Columns>
</asp:GridView>
背后的代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<string> competitors = Request.Cookies["TauLucompetitors"].Value.ToString().Select(x => x.ToString()).ToList();
TauluGridcompetitors.DataSource = competitors;
TauluGridcompetitors.DataBind();
GridView temp = (GridView)this.FindControl("TauluGridcompetitors");
**DropDownList DDL1 = (DropDownList)TauluGridcompetitors.FindControl("NoWeaponDDL");**
DDL1.DataSource = BindXml("XML/NoWeapon.xml");
DDL1.DataTextField = "Type";
DDL1.DataValueField = "ID";
DDL1.DataBind();
}
}
private DataSet BindXml(string fileName)
{
string filePath = "~/" + fileName;
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(filePath));
return ds;
}
可能是什么问题?
答案 0 :(得分:0)
您应该将数据绑定到 RowDataBound 事件中的下拉列表,并使用 ItemTemplate 而不是 EditItemTemplate 。
<强> ASPX 强>
<asp:GridView ID="TauluGridcompetitors" OnRowDataBound="TauluGridcompetitors_RowDataBound" runat="server">
<Columns>
<asp:TemplateField HeaderText="No Weapon">
<ItemTemplate>
<asp:DropDownList ID="NoWeaponDDL" runat="server" AutoPostBack="true"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Short Weapon"></asp:TemplateField>
<asp:TemplateField HeaderText="Long Weapon"></asp:TemplateField>
<asp:TemplateField HeaderText="Dual Weapon"></asp:TemplateField>
<asp:TemplateField HeaderText="Group Weapon"></asp:TemplateField>
</Columns>
</asp:GridView>
代码背后
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<string> competitors = Request.Cookies["TauLucompetitors"].Value.ToString().Select(x => x.ToString()).ToList();
TauluGridcompetitors.DataSource = competitors;
TauluGridcompetitors.DataBind();
}
}
protected void TauluGridcompetitors_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList DDL1;
if (e.Row.RowType == DataControlRowType.DataRow)
{
DDL1 = (DropDownList)e.Row.FindControl("NoWeaponDDL");
DDL1.DataSource = BindXml("XML/NoWeapon.xml").Tables[0];
DDL1.DataTextField = "Type";
DDL1.DataValueField = "ID";
DDL1.DataBind();
}
}
private DataSet BindXml(string fileName)
{
string filePath = "~/" + fileName;
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(filePath));
return ds;
}