我希望将三个列字段显示为下拉列表,其他显示为文本框格式。我已编写代码以显示数据库中的下拉列表值,但它无法正常工作。我已附加了我的代码以供您参考
<asp:GridView ID="Gv1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="FacultyName">
<ItemTemplate>
<asp:Label ID="lblfaculty" runat="server" Text='<%%# Eval("facultyname") %>>' Visible="false" />
<asp:DropDownList ID="ddlfaculty" runat="server" OnSelectedIndexChanged="ddlfaculty_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subject">
<ItemTemplate>
<asp:Label ID="lblsubject" runat="server" Text='<%%# Eval("subject") %>>' Visible="false" />
<asp:DropDownList ID="ddlsubject" runat="server" OnSelectedIndexChanged="ddlsubject_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subject">
<ItemTemplate>
<asp:Label ID="lblsubject" runat="server" Text='<%%# Eval("subject") %>>' Visible="false" />
<asp:DropDownList ID="ddlsubject" runat="server" OnSelectedIndexChanged="ddlsubject_SelectedIndexChanged1"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblsubject" runat="server" Text='<%%# Eval("sethour") %>>' Visible="false" />
<asp:TextBox ID="ddlsethour" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblcount" runat="server" Visible="false" />
<asp:TextBox ID="Count" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
public partial class transhonorarium : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ddlfaculty_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
SqlDataAdapter cmd = new SqlDataAdapter("select facultyname from faculty", con);
DataTable dt = new DataTable("dt");
cmd.Fill(dt);
Gv1.DataSource = dt;
Gv1.DataBind();
}
}
protected void ddlsubject_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
SqlDataAdapter cmd = new SqlDataAdapter("select subject from assign where facultyname=@facultyname", con);
DataTable dt = new DataTable("dt");
cmd.Fill(dt);
Gv1.DataSource = dt;
Gv1.DataBind();
}
}
}
答案 0 :(得分:0)
当数据发生变化时,请调用Gridview的DataBind()
方法。
我做这样的事情:
protected override void OnPreRender(Eventargs e)
{
base.OnPreRender(e);
GV1.DataBind();
}
答案 1 :(得分:0)
你需要在这里改变一件事。
AutoGenerateColumns = true
。因此,您将能够看到分配的列及其数据。其次,
您需要Bind
在Page_load
或您想要的任何地方protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Gv1.DataSource = Somesource;
Gv1.DataBind();
}
}
。这是页面加载。
Bind Data
请参阅public void BindData()
{
SqlCommand comd = new SqlCommand("SELECT * FROM Yourtablename", con);
SqlDataAdapter da = new SqlDataAdapter(comd);
DataTable dt = new DataTable();
da.Fill(dt);
GV1.DataSource = dt;
GV1.DataBind();
}
{{1}}
希望有所帮助
答案 2 :(得分:0)
为了填充您的下拉列表,您应该实现_RowDataBound事件并根据您的选择更新数据集基础,您还应该为您的下拉对象实现RowCommand事件。
顺便说一句。在ddlsubject_SelectedIndexChanged中,您还需要在sql命令中添加参数以使您的sql字符串正常工作,
希望这会有所帮助。