Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 7-May-14 23:25pm
v3

1 solution

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 :-
C#
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
 
Share this answer
 
Comments
[no name] 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)



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