Set Values for other columns in gridview based on selected value of dropdownlist inside gridview(edit mode) in ASP.Net
I have gridview with Employee Name, Emp Code, Designation. I am populating Employee Names dropdownlist with values in rowdatabound event. In edit mode of, on selection of a value in Employee Names dropdownlist, the EmpCode and Designation should change accordingly. The empCode and Designation label controls are in templatefield. I have writtern selectionchanged event for the Employee Names drodownlist,
ddlEmp.SelectedIndexChanged += new EventHandler(grd_ddlEmp_SelectedIndexChanged);
but I do not know how to change Emp Code and Designation va开发者_开发知识库lues inside the particular row in gridview.
Does these things inside the SelectedIndexChanged
event of the DropDownList
- First find the row.
- Then access the controls and change accordingly.
Pseudo Code
protected void grd_ddlEmp_SelectedIndexChanged(object sender, EventArgs e)
{
GridView row = ((DropDownList)sender).NamingContainer as GridViewRow;
Label Designation = row.FindControl("id of the designation label") as Label;
Designation.Text = "new Designation Name";
}
In the SelectedIndexChanged function, find the selected value of the drop down box
DropDownList ddl = (GridView1.Rows[GridView1.SelectedIndex].FindControl("DropDownList1") AS DropDownList);
var selectedVal = ddl.SelectedValue;
Execute some code to determine the Emp Code and Desgination and then populate the relevant label ( or other control):
Label lbl = GridView1.Rows[GridView1.SelectedIndex].FindControl("Label1") as Label;
Assign value to the label.
You need to put your gridview in UpdatePanel and do it in code behind of SelectedIndexChanged event like I have done in datalist below :
<asp:DataList ID="dlstPassengers" runat="server" OnItemDataBound="dlstPassengers_ItemDataBound"
RepeatDirection="Horizontal" RepeatColumns="2" Width="100%">
<ItemTemplate>
<div class="form-linebg" style="line-height: 32px;">
<asp:DropDownList ID="ddlCountry" AutoPostBack="true" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged"
CssClass="select-3" Style="width: 145px; margin-bottom: 9px;" runat="server">
</asp:DropDownList>
<br />
<asp:DropDownList ID="ddlCity" CssClass="select-3" Style="width: 145px; margin-bottom: 10px;"
runat="server">
</asp:DropDownList>
</div>
</ItemTemplate>
</asp:DataList>
In code behind :
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlCountry = (DropDownList)sender;
DropDownList ddlCity = (DropDownList)((DropDownList)sender).Parent.FindControl("ddlCity");
BindCity(ddlCity, ddlCountry.SelectedValue);
}
private void BindCity(DropDownList ddlCity, string countryCode)
{
// Binding code of city based selected country
}
You need to set EmpCode and Designation columns on SelectedIndexChanged event of dropdown by finding control in code behind.
use in your dropdown AutoPostBack="true"
精彩评论