2011-11-24 56 views
2

我有一个DropDownListGridView。我在GridView RowCreated()中动态填写GridView。当我点击GridView的命令按钮时,我想获得DropDownList的值。Gridview的下拉列表丢失RowCommand

RowCommand函数中,我试图获得该值。但是,我无法在该功能中找到控件。令人惊讶的是,另一个DropDownList在相同的功能中工作正常。

C#代码如下:

if (Request.Params["ID"] != null && Request.Params["ID"] != "") 
{ 
    FirmID = Convert.ToInt32(Request.Params["ID"]); 
    //OfficeList = IFARecord.GetOffices(FirmID);  
} 
if (!IsPostBack) 
{ 
    GV_SABAdvisers.DataSourceID = "objectDataSourceSAV"; 
    GV_SABAdvisers.DataBind(); 
} 

protected void GV_SABAdvisers_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "Index") 
    { 
     int Index = Convert.ToInt32(e.CommandArgument); 
     GridViewRow row = GV_SABAdvisers.Rows[Index]; 

     //Not working 
     DropDownList Offices = row.FindControl("ddlLocation") as DropDownList; 
     String officeID = (Offices is DropDownList) ? Offices.SelectedValue : null; 

     DropDownList ddlJobTitle = row.FindControl("ddlJobTitle") as DropDownList; 

     if (ddlJobTitle is DropDownList) 
     { 
      newAdviserJobTitle.JobTitleID = Convert.ToInt32(ddlJobTitle.SelectedValue);// this works fine. 
     } 
    } 
}  

protected void GV_SABAdvisers_RowCreated(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     DropDownList ddlLocation = e.Row.FindControl("ddlLocation") as DropDownList; 
     DropDownList ddlJobTitle = e.Row.FindControl("ddlJobTitle") as DropDownList; 
     DataTable OfficeList = GetOffices(FirmID); 
     DataTable dtJob = MyTouchstone.Advisers.AdviserFirmJobTitle.AllJobTitle(); 

     foreach (DataRow aOffice in OfficeList.Rows) 
     { 
      ddlLocation.Items.Add(new ListItem(aOffice["Address1"].ToString() + ", " + aOffice["Postcode"].ToString(), aOffice["OfficeID"].ToString())); 
     } 

     foreach (DataRow aJob in dtJob.Rows) 
     { 
      ddlJobTitle.Items.Add(new ListItem(aJob["JobTitle"].ToString(), aJob["JobTitleID"].ToString())); 
     } 
    } 
} 

ASP标记:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="True"> 
    <ContentTemplate> 
     <asp:GridView ID="GV_SABAdvisers" AutoGenerateColumns="false" PageSize = "10" 
      CssClass="cssPager" AllowPaging="true" AllowSorting="true" 
      OnPageIndexChanging="GV_SABAdvisers_PageIndexChanging" 
      runat="server" onrowcommand="GV_SABAdvisers_RowCommand" 
      onrowcreated="GV_SABAdvisers_RowCreated"> 
      <PagerStyle /> 
      <Columns> 
       <asp:TemplateField HeaderText="Job Title"> 
        <ItemStyle Width="80px" /> 
        <ItemTemplate> 
         <asp:DropDownList ID="ddlJobTitle" Width="80px" runat="Server"></asp:DropDownList> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="Location"> 
        <ItemStyle Width="200px" /> 
        <ItemTemplate> 
         <asp:DropDownList ID="ddlLocation" Width="80px" runat="Server"></asp:DropDownList> 
        </ItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
     </asp:GridView> 

谁能帮助我,好吗?

+0

请提供您的ASP.NET标记 - 我将重点讨论ddlLocation和ddlJobTitle之间的标记差异,因为一个可以工作,一个不可以。 –

+0

在'RowCreated'事件处理程序中,您可以在不调用'e.Row.FindControl'方法的情况下访问'ddlLocation'。它放置在GridView中吗? –

+0

Plz现在看到Asp标记。两个控件都是一样的。我只绑定一次数据。 – Shuvra

回答

0

我没有看到你在源代码中使用CommandName“Index”的位置。