У меня есть 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>
Может кто-нибудь помочь мне, пожалуйста?