Click here to Skip to main content
13,249,813 members (67,829 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Dear All,
I am using label in itemtemplate and in edititemtemplate, I am using dropdownlist. When I click on edit link in gridview, I want to show Dropdownlist with label's text as selected value with other database values. I tried many solutions, but didn't work. I have set autopostback true of dropdown. Here is my code -

protected void gvTransport_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && gvTransport.EditIndex == e.Row.RowIndex)
{
/*... For getting Route Names from dbo.Transport_Routes table ...*/
DropDownList RouteName = (DropDownList)e.Row.FindControl("ddlRoute_ID");
Label lblRName = (Label)e.Row.FindControl("lblRoute_ID");

RouteName.DataSource = LoadStudentRoutes(); // To Get Route Names
RouteName.DataTextField = "Route_Name";
RouteName.DataValueField = "Route_ID";
RouteName.DataBind();

RouteName.Items.FindByText(lblRName.Text).Selected = true;
}
}

but edit event is not fired, when I click edit link. Please help me in this situation.
Thanks and regards,
Rizwan Gazi.
Posted 7-May-14 22:44pm
Updated 8-May-14 0:25am
v3

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi Rizwan, Here is the solution:-
1. Firstly, Set autopostback of dropdownlist to "False"
2. With the use of RowCommand Event of the Grid View , Get the value of the Edited Label into the ViewState, As below:-

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            GridViewRow gvRow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
 
            Label lbl = (Label)gvRow.FindControl("Label1");
            ViewState["LabelValue"] = lbl.Text;
            
        }
    }


3. Finally, Use viewstate and Handle your RowDataBound Event as below :-
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)
       {
           //Label lbl = (Label)e.Row.FindControl("Label1");
           DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
           ddl.SelectedValue = ddl.Items.FindByText(Convert.ToString(ViewState["LabelValue"])).Value;
       }
   }


4. Your Job Is done !!!

~Thanks
  Permalink  
Comments
Dnyaneshwar@Pune 8-May-14 7:16am
   
good solution Jagbir Saini
Jagbir Saini 8-May-14 7:29am
   
Thanxs :)
Rizwan Gazi 8-May-14 8:10am
   
Thank you so much dear. Your solution worked perfect. I Searched many answers for this, but none suggested me of RowCommand event. I am very thankful of you to bring me out of the situation. Once again many thanks dear @Jagbir Saini for answering the question.
Jagbir Saini 8-May-14 8:20am
   
Welcome Rizwan

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.171114.1 | Last Updated 8 May 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100