如何在不使用sqldatasource的情况下在datalistview中绑定Dropdownlist? [英] How to bind Dropdownlist in datalistview without using sqldatasource?
问题描述
我正在创建一个应用程序.我在绑定编辑模板的下拉列表中的数据时遇到问题.我正在使用三层体系结构.我正在使用对象属性.
我只是在运行时显示框,但其中没有任何值.
您为此编写了代码吗?
如果有的话,请给我发邮件
**已移除**
I am creating a application.i have a problem to bind data in dropdownlist in edit template.I am using 3-tier architecture.I am using object properties.
I just display my box at runtime but no value in it.
Have you code for that?
If you have then please mail me
**Removed**
推荐答案
在Gridview的RowDatabound事件发生时尝试绑定下拉列表....希望这将为您提供解决方案......
Try to bind the dropdown while RowDatabound event of the Gridview....hope this will give u the solution ......
protected void grdTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList drpBuildServers = new DropDownList();
if (grdTest.EditIndex == e.Row.RowIndex)
{
drpBuildServers = (DropDownList)e.Row.Cells[0].FindControl("ddlBuildServers");
}
}
尝试此操作可能会帮助您.....
用户界面
-----
Try this may be it will helps You.....
UI
-----
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# bind("country")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlCountry" runat="server"
AppendDataBoundItems="True" >
<asp:ListItem Value="" Text="" />
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
=======
背后的代码
--------
=======
Code Behind
--------
/// <summary>
/// Change DeailsView mode to Edit
///and data bind
/// Change DetailsView mode to Edit
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnEditUser_Click(object sender, EventArgs e)
{
dvUserRecord.ChangeMode(DetailsViewMode.Edit);
//Bind DetailsView with your method
dvUserRecord.DataSource = GetUserRecord();
dvUserRecord.DataBind();
}
protected void dvUserRecord_DataBound(object sender, EventArgs e)
{
if (dvUserRecord.CurrentMode == DetailsViewMode.Edit)
{
//Find Drop down list from aspx page
DropDownList ddlCountry = dvUserRecord.FindControl("ddlCountry")
as DropDownList;
//check ddlCountry is not null || country drop down list is found
if(ddlCountry != null)
{
//Bind countries data to ddlCountry
ddlCountry.DataTextField = "countryText";
ddlCountry.DataValueField = "IndexValue";
ddlCountry.DataSource = GetCountries;
//custom method that gets all countries
ddlCountry.DataBind();
}
}
Good Day Priyadarshini.Thokala
我的UI代码
Good Day Priyadarshini.Thokala
my UI code
<asp:DataList ID="dl_profile" runat="server"
oncancelcommand="dl_profile_CancelCommand"
oneditcommand="dl_profile_EditCommand"
onupdatecommand="dl_profile_UpdateCommand"
DataKeyField="pk_registeration_id" onitemcommand="dl_profile_ItemCommand">
<HeaderTemplate><table width="408" border="0" cellspacing="0" cellpadding="0" class="formstyle"></HeaderTemplate>
<ItemTemplate>
<tr>
<td width="82" height="25"> Name :</td>
<td colspan="3" style=" border-removeddotted 1px #999999"><%#Eval("registeration_name")%></td>
</tr>
<tr>
<td height="25"> Email :</td>
<td colspan="3" style=" border-removeddotted 1px #999999"><%#Eval("registeration_email")%></td>
</tr>
<tr>
<td height="25"> DOB :</td>
<td colspan="3" style=" border-removeddotted 1px #999999"><%#Eval("registeration_datefbirth")%></td>
</tr>
<tr>
<td height="25"> Profession :</td>
<td colspan="3" style=" border-removeddotted 1px #999999"><%#Eval("registeration_profession")%></td>
</tr>
<tr>
<td height="25"> City :</td>
<td colspan="3" style=" border-removeddotted 1px #999999"><%#Eval("cityname")%>
<%--<asp:TextBox ID="TextBox2" MaxLength="100" runat="server" Text='<%#Eval("cityname")%>'></asp:TextBox>--%>
</td>
</tr>
<tr>
<td height="18"></td>
<td colspan="3">
</td>
</tr>
<tr>
<td> </td>
<td >
<asp:ImageButton ID="imgbtn_edit" runat="server" ImageUrl="../images/editprofile.jpg" width="65px" height="18px" CommandName="edit" />
<asp:ImageButton ID="imgbtn_ok" runat="server" ImageUrl="../images/send.jpg" width="65px" height="18px" border="0" CommandName="ok" /></td>
<td > </td>
<td ></td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td width="82" height="25"> Name :</td>
<td colspan="3" style=" border-removeddotted 1px #999999">
<asp:TextBox ID="txt_name" runat="server" MaxLength="35" Text='<%#Eval("registeration_name")%>'></asp:TextBox></td>
</tr>
<tr>
<td height="25"> Email :</td>
<td colspan="3" style=" border-removeddotted 1px #999999"><asp:TextBox ID="txt_email" MaxLength="100" runat="server" Text='<%#Eval("registeration_email")%>'></asp:TextBox></td>
</tr>
<tr>
<td height="25"> DOB :</td>
<td colspan="3" style=" border-removeddotted 1px #999999"><asp:TextBox ID="txt_dob" MaxLength="10" runat="server" Text='<%#Eval("registeration_datefbirth")%>'></asp:TextBox>
<asp:ImageButton ID="imgbtn_calender" runat="server" ImageUrl="../images/cal.gif" />
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" PopupButtonID="imgbtn_calender" TargetControlID="txt_dob" Format="dd/MM/yyyy" >
</cc1:CalendarExtender>
</td>
</tr>
<tr>
<td height="25"> Profession :</td>
<td colspan="3" style=" border-removeddotted 1px #999999"><asp:TextBox ID="TextBox1" MaxLength="100" runat="server" Text='<%#Eval("registeration_profession")%>'></asp:TextBox></td>
</tr>
<tr>
<td height="25"> City :</td>
<td colspan="3" style=" border-removeddotted 1px #999999"><%--<asp:TextBox ID="TextBox2" MaxLength="100" runat="server" Text='<%#Eval("cityname")%>'></asp:TextBox>--%>
<asp:DropDownList ID="ddlcity" runat="server" AppendDataBoundItems="True"><asp:ListItem Value="" Text="" /></asp:DropDownList>
</td>
</tr>
<tr>
<td height="28"> Image :</td>
<td colspan="3">
<asp:FileUpload ID="FileUpload1" runat="server" /></td>
</tr>
<tr>
<td height="10"></td>
<td colspan="3">
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:ImageButton ID="imgbtn_update" runat="server" ImageUrl="~/images/update.jpg" width="65" height="18" CommandName="update" />
<asp:ImageButton ID="imgbtn_cancel" runat="server" ImageUrl="~/images/Cancel.jpg" width="65" height="18" CommandName="cancel" />
</td>
<td > </td>
<td ></td>
</tr>
</EditItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
Code behind File''s Code is
protected void dl_profile_EditCommand(object source, DataListCommandEventArgs e)
{
dl_profile.EditItemIndex = e.Item.ItemIndex;
objProp.Action = "1";
binddata();
bindImage();
}
public void bindImage()
{
int result = 0;
objProp.Dataset = blogic.Fetch_registration(ref result, objProp);
dl_profile_img.DataSource = objProp.Dataset;
dl_profile_img.DataBind();
}
public void binddata()
{
int result = 0;
objProp.Dataset = blogic.Fetch_registration(ref result, objProp);
dl_profile.DataSource = objProp.Dataset;
dl_profile.DataBind();
}
protected void dl_profile_ItemDataBound1(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
DropDownList ddl = e.Item.FindControl("ddlcity") as DropDownList;
if (ddl != null)
{
lstitem.Clear();
lstitem = blogic.Citydata(objProp);
blogic.fills((DropDownList)e.Item.FindControl("ddlcity"), lstitem);
}
}
}
Class code
public void fills(DropDownList dp, List<ListItem> lstitem)
{
try
{
for (int i = 0; i < lstitem.Count; i++)
{
ListItem li = new ListItem();
li.Text = lstitem[i].Text.ToString();
li.Value = lstitem[i].Value.ToString();
dp.Items.Add(li);
}
}
catch (Exception ex)
{ }
}
public List<ListItem> Citydata(Property objProp)
{
objProp.City = "";
if (objProp.City != "")
objProp.Query = "select a.pk_cityid,a.cityname,b.registeration_city from city a inner join registration b on b.registeration_city = a.pk_cityid where a.registeration_userid='" + objProp.Userid + "'";
else
objProp.Query = "select * from city";
objProp.Reader = DataLayer.ExecuteReader(objProp.Connection, CommandType.Text, objProp.Query);
ListItem li = new ListItem();
li.Attributes.Clear();
li.Text = "Select";
li.Value = "-1";
lstitem.Add(li);
while (objProp.Reader.Read())
{
li = new ListItem();
li.Text = objProp.Reader["cityname"].ToString();
li.Value = objProp.Reader["pk_cityid"].ToString();
lstitem.Add(li);
}
objProp.Reader.Close();
return lstitem;
}
public DataSet Fetch_registration(ref int result, Property objProp)
{ try
{
if (objProp.Action == "1")
{
objProp.Query = "SELECT reg.pk_registeration_id,reg.registeration_userid,reg.registeration_name,reg.registeration_email,reg.registeration_datefbirth,reg.registeration_uimage,reg.registeration_profession FROM registeration AS reg INNER JOIN city AS ct ON ct.pk_cityid = reg.registeration_city WHERE reg.registeration_userid='" + objProp.Userid + "' ";
}
else { objProp.Query = "SELECT reg.pk_registeration_id,reg.registeration_userid,reg.registeration_name,reg.registeration_email,reg.registeration_datefbirth,reg.registeration_uimage,ct.cityname,reg.registeration_profession FROM registeration AS reg INNER JOIN city AS ct ON ct.pk_cityid = reg.registeration_city WHERE reg.registeration_userid='" + objProp.Userid + "' "; }
objProp.Dataset = DataLayer.ExecuteDataset(objProp.Connection, CommandType.Text, objProp.Query);
}
catch (Exception ex)
{ }
return objProp.Dataset;
}
There are 2 tables
1 have value of cityid,stateid and city name <-table name city
2 have value of profile details like city,proffession,etc.. <-table name registration
It''s working nice...
There are 2 tables
1 have value of cityid,stateid and city name <-table name city
2 have value of profile details like city,proffession,etc.. <-table name registration
It''s working nice...
这篇关于如何在不使用sqldatasource的情况下在datalistview中绑定Dropdownlist?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!