在C#中将下拉列表绑定到编辑模板中 [英] Bind the dropdown in edit template in c#
本文介绍了在C#中将下拉列表绑定到编辑模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HI,
我在将我的下拉列表绑定到编辑模板中时遇到问题..我尝试了很多但无法做到这一点.请帮助.....
HI ,
i am having problem in binding my dropdown in edit template ....i have tried many and can''t do it.plz help .....
<asp:TemplateField HeaderText="State">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("State")%>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("State")%>' Visible = "false"></asp:Label>
<asp:DropDownList ID="ddlStates" runat="server" Width="70px" ></asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlState" runat="server" Width="70px" ></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)
{
DropDownList ddlState = (DropDownList)e.Row.FindControl("ddlState");
string query = "SELECT StateID,StateName from STCState";
SqlCommand cmd = new SqlCommand(query);
ddlState.DataSource = GetAllStates(cmd);
//ddlState.DataTextField = "StateName";
//ddlState.DataValueField = "StateID";
ddlState.DataBind();
ddlState.Items.FindByValue((e.Row.FindControl("Label2") as Label).Text).Selected = true;
}
}
我的IF条件总是错误,当我单击编辑时就不会进入IF条件....
My IF CONDITION ALWAYS GET FALSE IT DOES NOT GO IN TO IF CONDITION WHEN I CLICK ON EDIT.......
public DataTable GetAllStates(SqlCommand cmd)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=JEEVAN01\\SQLEXPRESS; Initial catalog =suresh; Integrated Security=true";
conn.Open();
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = conn;
sda.SelectCommand = cmd;
using ( DataTable DT=new DataTable())
{
sda.Fill(DT);
return DT;
}
}
}
在此先感谢
suresh
Thanks in Advance
suresh
推荐答案
非常简单.您做错了,因为该事件导致控件不存在.
Quite easy. You''re doing it wrong, cause by that Event the control is not there.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow &&
e.Row.RowState == DataControlRowState.Edit)
{
// Here you will get the Control you need like:
DropDownList dl = (DropDownList)e.Row.FindControl("ddlState");
// Bind this DropDownList here..
}
}
--Amit
--Amit
这篇关于在C#中将下拉列表绑定到编辑模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文