I would like to populate a ddl which is based on the preceeding label text when Editing. But whenever I press the edit button, the event just doesn't fire up. Anyone knows why?
My codes are as follows:
My SP:
IF @Master = 'ac_code_ccy_code'
SELECT ac_code FROM table
WHERE ccy_code=@key
Select your Gridview and view properties. In the properties diaglogue select events and check whether your row data bound event is associated with your gridview or not
Please click "Mark as Answer" if you think this post answer your question
Please use gridview_rowediting event for that purpose. In the row editing event you get the row of the current edit item, find the dropdown control and then bind the dropdown
As you can see in the abovementioned codes, I tried to do it in the Rowdatabound, but nothing fires when the edit button is pressed. I don't know what's wrong with this.
Are you saying that I should write similar codes in the rowediting event?
Do not missing onrowediting="GridView1_RowEditing" on GridView tag
If you need to rebind the DropDownList:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Get data source to rebind GridView
DropDownList dropdown = GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1") as DropDownList;
Get data source rebind DropDownList
}
Please mark the replies as answers if they help or unmark if not.
Feedback to us
henrytong
Member
4 Points
19 Posts
How can I bind the ddl in the Edit ItemTemplate of GridView with the selected key which is a labe...
Oct 12, 2012 06:59 AM|LINK
Dear all,
I would like to populate a ddl which is based on the preceeding label text when Editing. But whenever I press the edit button, the event just doesn't fire up. Anyone knows why?
My codes are as follows:
My SP: IF @Master = 'ac_code_ccy_code' SELECT ac_code FROM table WHERE ccy_code=@keyMy CS:
protected void GV_RowDataBound(object sender, GridViewRowEventArgs e) { if (GV.EditIndex > -1 && e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.RowIndex == GV.EditIndex) { DataRowView gr = (DataRowView)e.Row.DataItem; Label lblccy_code = (Label)e.Row.Cells[0].FindControl("lblccy_code"); DropDownList ddlequ_ac_code = (DropDownList)e.Row.Cells[1].FindControl("ddlequ_ac_code"); ObjectDataSource ods_upd_equ_ac_code = (ObjectDataSource)e.Row.Cells[1].FindControl("ods_upd_equ_ac_code"); ods_upd_equ_ac_code.SelectParameters["Key"].DefaultValue = lblccy_code.Text.Trim(); ddlequ_ac_code.DataSource = JALISTPE.DataAccess.CodeList.GetMasterData(_user.CompCode.Trim(), _user.UserID.Trim(), "ac_code_ccy_code", lblccy_code.Text.Trim()); ddlequ_ac_code.DataTextField = "DisplayMember1"; ddlequ_ac_code.DataValueField = "ValueMember"; ddlequ_ac_code.DataBind(); } } public static List<ArrayHelper> GetMasterData(string CompCode, string UserId, string Master, string Key) { List<ArrayHelper> objs = new List<ArrayHelper>(); objs.Add(new ArrayHelper("", "", "")); // * Default Value SqlConnection sqlConn = new SqlConnection(ConfigurationManager.AppSettings["cnn_string"]); string sSql = "sp_GetMasterDropdown"; SqlCommand sqlCmd = new SqlCommand(sSql, sqlConn); sqlCmd.CommandType = CommandType.StoredProcedure; sqlCmd.Parameters.AddWithValue("@CompCode", CompCode); sqlCmd.Parameters.AddWithValue("@Master", Master); sqlCmd.Parameters.AddWithValue("@UserId", UserId); sqlCmd.Parameters.AddWithValue("@Key", Key); try { if ((sqlConn.State != System.Data.ConnectionState.Open)) { sqlConn.Open(); } System.Data.SqlClient.SqlDataReader dr = sqlCmd.ExecuteReader(); for ( ; dr.Read(); ) { ArrayHelper obj = new ArrayHelper(dr[0].ToString(), dr[1].ToString(), dr[2].ToString()); objs.Add(obj); } dr.Close(); } catch (System.Exception e) { DBErrorLog objDBErrorLog = new DBErrorLog(UserId, e.GetType().ToString(), e.ToString(), "CodeList.aspx.cs", "GetMasterData()"); objDBErrorLog.WriteToLog(); } finally { if ((sqlConn.State != System.Data.ConnectionState.Closed)) { sqlConn.Close(); } sqlCmd.Dispose(); } return objs; } }<asp:TemplateField HeaderText="ccy_code"> <ItemTemplate> <asp:Label ID="lblccy_code" runat="server" Text='<%# Eval("ccy_code")%>' /> </ItemTemplate> <EditItemTemplate> <asp:label id="lblccy_code" runat="server" Text='<%#Eval("ccy_code") %>'></asp:label> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="equ_ac_code"> <ItemTemplate> <asp:Label ID="lblequ_ac_code" runat="server" Text='<%# Eval("equ_ac_code")%>' /> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID="ddlequ_ac_code" runat="server" DataSourceID="ods_equ_ac_code" DataTextField="DisplayMember1" DataValueField="ValueMember" SelectedValue='<%# Eval("equ_ac_code")%>' DatakeyCssClass="UIPageFieldList" /> <asp:ObjectDataSource ID="ods_upd_equ_ac_code" runat="server" SelectMethod="GetMasterData" TypeName="JALISTPE.DataAccess.CodeList" > <SelectParameters> <asp:ControlParameter Name="CompCode" ControlID="hf_CompCode" PropertyName="Value" /> <asp:ControlParameter Name="UserId" ControlID="hf_UserId" PropertyName="Value" /> <asp:Parameter Name="Master" DefaultValue="ac_code_ccy_code" /> <asp:ControlParameter Name="Key" ControlID="lblccy_code" PropertyName="Text.Trim()" /> </SelectParameters> </asp:ObjectDataSource> </EditItemTemplate> </asp:TemplateField>sureshkumar....
Contributor
2143 Points
504 Posts
Re: How can I bind the ddl in the Edit ItemTemplate of GridView with the selected key which is a ...
Oct 12, 2012 07:41 AM|LINK
which event is not firing?
Best Regards,
Suresh Kumar Gundala
oned_gk
All-Star
31782 Points
6496 Posts
Re: How can I bind the ddl in the Edit ItemTemplate of GridView with the selected key which is a ...
Oct 12, 2012 07:45 AM|LINK
simply bind selectedvalue='<%# Bind("equ_ac_code")%>'
Add hiddenfieldccy_code in edititemtemplate bind the value property similar bind with label text property
Use the HF as controlparameter
If not work try switch hiddenfieldccy_code bind mode between EVAL and BIND.
henrytong
Member
4 Points
19 Posts
Re: How can I bind the ddl in the Edit ItemTemplate of GridView with the selected key which is a ...
Oct 12, 2012 08:38 AM|LINK
Dear Suresh ,
Sorry. I mean the rowdatabound event just doesn't fire up
Henry
sureshkumar....
Contributor
2143 Points
504 Posts
Re: How can I bind the ddl in the Edit ItemTemplate of GridView with the selected key which is a ...
Oct 12, 2012 08:48 AM|LINK
Dear Henry,
Select your Gridview and view properties. In the properties diaglogue select events and check whether your row data bound event is associated with your gridview or not
Best Regards,
Suresh Kumar Gundala
oned_gk
All-Star
31782 Points
6496 Posts
Re: How can I bind the ddl in the Edit ItemTemplate of GridView with the selected key which is a ...
Oct 12, 2012 08:50 AM|LINK
In rowdatabound event i'm not sure you can use if condition : if (GV.EditIndex > -1 .... try remove it.
Anotherway, you can use rowediting event to bind ddl using neweditindex value
sureshkumar....
Contributor
2143 Points
504 Posts
Re: How can I bind the ddl in the Edit ItemTemplate of GridView with the selected key which is a ...
Oct 12, 2012 09:13 AM|LINK
Henry,
Please use gridview_rowediting event for that purpose. In the row editing event you get the row of the current edit item, find the dropdown control and then bind the dropdown
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { }Best Regards,
Suresh Kumar Gundala
henrytong
Member
4 Points
19 Posts
Re: How can I bind the ddl in the Edit ItemTemplate of GridView with the selected key which is a ...
Oct 15, 2012 03:06 AM|LINK
Thank you for all the instructive comments.
As you can see in the abovementioned codes, I tried to do it in the Rowdatabound, but nothing fires when the edit button is pressed. I don't know what's wrong with this.
Are you saying that I should write similar codes in the rowediting event?
Henry
Frank Jiang ...
All-Star
16006 Points
1728 Posts
Microsoft
Re: How can I bind the ddl in the Edit ItemTemplate of GridView with the selected key which is a ...
Oct 15, 2012 05:24 AM|LINK
Rowdatabound event occurs when a data row is bound to data in a GridView control.
RowEditing event occurs when a row's edit button is clicked.
If you edit the GridView row, please put your code in GridView.RowEditing Event
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { Your code logic }Do not missing onrowediting="GridView1_RowEditing" on GridView tag
If you need to rebind the DropDownList:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; Get data source to rebind GridView DropDownList dropdown = GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1") as DropDownList; Get data source rebind DropDownList }Feedback to us
Develop and promote your apps in Windows Store
sarathi125
Star
13599 Points
2691 Posts
Re: How can I bind the ddl in the Edit ItemTemplate of GridView with the selected key which is a ...
Oct 15, 2012 06:40 AM|LINK
Hi,
Check here,
http://www.myaspsnippets.blogspot.co.uk/2010/08/gridview-with-dropdownlist-in-all-type.html
Remember to click Mark as Answer on the post that helps to others.
My Blog :MyAspSnippets