【发布时间】:2011-11-24 14:38:35
【问题描述】:
我在GridView 中有一个DropDownList。我在GridView RowCreated() 中动态填充GridView。当我单击GridView 的命令按钮时,我想获得DropDownList 的值。
在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>
有人可以帮帮我吗?
【问题讨论】:
-
请提供您的 ASP.NET 标记 - 我将重点关注 ddlLocation 与 ddlJobTitle 标记的差异,因为一个有效,一个无效。
-
在
RowCreated事件处理程序中,您无需调用e.Row.FindControl方法即可访问ddlLocation。是否放在 GridView 中? -
请立即查看 Asp 标记。两者的控制是一样的。而且我只绑定一次数据。
-
嗨 Yuriy,ddlLocation 也在使用 e.Row.FindControl 进行访问。我修改代码。我错过了在帖子中提交的内容。我再次修改代码。谢谢
-
复制并粘贴
RowCommand事件处理程序的完整代码。你从哪里得到row对象?
标签: asp.net gridview postback drop-down-menu