Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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**
Posted
Updated 2-Feb-12 0:53am
v7

Good Day Priyadarshini.Thokala

my UI code
ASP.NET
<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
C#
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...
 
Share this answer
 
v3
Comments
shahviral 3-Feb-12 7:05am    
I got My solution .......Use Above code ...
If any query then post here.....
Try this may be it will helps You.....


UI
-----
ASP.NET
<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
--------
C#
/// <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();
}
}
 
Share this answer
 
v2
Comments
shahviral 3-Feb-12 0:28am    
what is dvUserRecord?
Priyadarshini.Thokala 6-Feb-12 6:42am    
This is name of datalistview
Priyadarshini.Thokala 6-Feb-12 6:41am    
shahviral asked:
hey first of all thanks for replying Priyadarshini.Thokala

But I am using datalist view not a gridview..

If you have then send me full code which is designed side and code behind file
hey first of all thanks for replying Priyadarshini.Thokala

But I am using datalist view not a gridview..

If you have then send me full code which is designed side and code behind file

**Removed**
 
Share this answer
 
v2
Comments
Priyadarshini.Thokala 6-Feb-12 6:41am    
You are not supposed to post a comment here, post under my answer, so that i will get a mail.
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");    
  }
}
 
Share this answer
 
v2
Comments
Sridhar Patnayak 2-Feb-12 5:48am    
Pre tags added
ponnam madhu 2-Feb-12 5:59am    
Hi...what is 'Continuous integration'?
please provide me sample how to use in .NET..?

Madhu

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900