2011-10-03 84 views
0

我有一个带有Employee Name,Emp Code,Designation的gridview。我正在使用rowdatabound事件中的值填充员工姓名下拉列表。在编辑模式中,在选择Employee Names下拉列表中的值时,EmpCode和Designation应该相应地改变。 empCode和Designation标签控件位于templatefield中。我已经writtern SelectionChanged事件的员工姓名drodownlist,在gridview中根据gridview(编辑模式)中dropdownlist的选定值为gridview中的其他列设置值

ddlEmp.SelectedIndexChanged += new EventHandler(grd_ddlEmp_SelectedIndexChanged); 

,但我不知道如何改变的Emp代码及名称值的特定行内的GridView控件。

回答

1

做这些事的SelectedIndexChanged事件DropDownList

  1. 首先发现里面的行。
  2. 然后访问控件并相应地更改。

伪码

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"; 
} 
1

你需要把你的GridView中的UpdatePanel,并做到在代码的背后的SelectedIndexChanged事件就像我在下面的DataList都做:

<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> 

在后面的代码:

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 
    } 

您需要通过在后面的代码中查找控件来设置下拉菜单中的SelectedIndexChanged事件的EmpCode和指定列。

+0

我能够得到的只有下拉列表的值的使用,我不能够使用访问GridView的任何其他列来自发件人的Parent.FindControl。纳文的解决方案解决了!感谢您的回答。 – banupriya

1

在的SelectedIndexChanged函数,查找下拉框

DropDownList ddl = (GridView1.Rows[GridView1.SelectedIndex].FindControl("DropDownList1") AS DropDownList); 
    var selectedVal = ddl.SelectedValue; 

的设定值执行一些代码来确定的Emp代码和Desgination然后填充相关的标签(或其它控制):

Label lbl = GridView1.Rows[GridView1.SelectedIndex].FindControl("Label1") as Label; 

将值赋给标签。

0

在下拉的AutoPostBack =“真”

相关问题