下拉列表在gridview RowCommand上丢失值

时间:2011-11-24 14:38:35

标签: asp.net gridview postback drop-down-menu

我在DropDownList中有一个GridView。我在GridView动态填写GridView RowCreated()。当我点击DropDownList的命令按钮时,我希望得到GridView的值。

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>

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

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