2011-10-03 44 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 =「真」

相關問題