所以我的网格视图处于编辑模式时会出现一个下拉列表。问题是,当我按下更新按钮时,我得到一个空引用错误。这是因为由于某种原因,更新事件声称网格视图中不存在下拉列表。当我看到标记时也可以这么说。但是,当我运行页面并按下编辑按钮时,我的下拉列表没有问题。谁能告诉我为什么会这样呢?
这是我到目前为止对更新事件的代码隐藏:
protected void GridViewHolder_Updating(object sender, GridViewUpdateEventArgs e)
{
int machineid1;
string machineid;
string machineTypeid;
string machineModelid;
GridViewRow row = (GridViewRow)GridViewHolder.Rows[e.RowIndex];
machineid = ((Label)(row.Cells[0].Controls[1])).Text;
machineid1 = Convert.ToInt32(machineid);
machineTypeid = ((DropDownList)(row.Cells[3].FindControl("MachineTypeDropDown"))).SelectedValue;
machineModelid = ((DropDownList)(row.Cells[4].Controls[1])).SelectedValue;
//inputsService.UpdateMachineTypes(machineid, machineTypeid);
//inputsService.UpdateMachineModels(machineid, machineModelid);
}
您将在下面找到标记和.ascx gridview标记:
viewsource:
<th scope="col">Site Name</th><th scope="col">Machine Name</th><th scope="col">Machine Type</th><th scope="col">Machine Model</th><th scope="col"> </th>
</tr><tr align="center" style="background-color:Transparent;">
<td>SACRAMENTO</td><td>DC04</td><td>
<span id="AssignMachineTypesAndModels_userControl_GridViewHolder_ctl02_MachineTypeLabel">empty</span>
</td><td>
<span id="AssignMachineTypesAndModels_userControl_GridViewHolder_ctl02_MachineModelLabel">empty</span>
</td><td align="center"><input type="button" value="Edit" onclick="javascript:__doPostBack('AssignMachineTypesAndModels_userControl$GridViewHolder','Edit$0')" /></td>
</tr><tr align="center" style="background-color:Transparent;">
<td>SACRAMENTO</td><td>DC1</td><td>
<span id="AssignMachineTypesAndModels_userControl_GridViewHolder_ctl03_MachineTypeLabel">empty</span>
</td><td>
<span id="AssignMachineTypesAndModels_userControl_GridViewHolder_ctl03_MachineModelLabel">empty</span>
</td><td align="center"><input type="button" value="Edit" onclick="javascript:__doPostBack('AssignMachineTypesAndModels_userControl$GridViewHolder','Edit$1')" /></td>
</tr><tr align="center" style="background-color:Transparent;">
<td>SACRAMENTO</td><td>MPS01</td><td>
<span id="AssignMachineTypesAndModels_userControl_GridViewHolder_ctl04_MachineTypeLabel">empty</span>
</td><td>
<span id="AssignMachineTypesAndModels_userControl_GridViewHolder_ctl04_MachineModelLabel">empty</span>
</td><td align="center"><input type="button" value="Edit" onclick="javascript:__doPostBack('AssignMachineTypesAndModels_userControl$GridViewHolder','Edit$2')" /></td>
</tr><tr align="center" style="background-color:Transparent;">
<td>SACRAMENTO</td><td>MSE01</td><td>
<span id="AssignMachineTypesAndModels_userControl_GridViewHolder_ctl05_MachineTypeLabel">empty</span>
</td><td>
<span id="AssignMachineTypesAndModels_userControl_GridViewHolder_ctl05_MachineModelLabel">empty</span>
</td><td align="center"><input type="button" value="Edit" onclick="javascript:__doPostBack('AssignMachineTypesAndModels_userControl$GridViewHolder','Edit$3')" /></td>
gridview标记:
<Columns>
<asp:TemplateField HeaderText="ID"
SortExpression="ID"
Visible="False">
<ItemTemplate>
<asp:Label ID="Label1"
runat="server"
Text='<%# Bind("ID") %>'
Visible="false">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="SiteName"
HeaderText="Site Name"
SortExpression="SiteName"
ReadOnly="true" />
<asp:BoundField DataField="Name"
HeaderText="Machine Name"
ReadOnly="true"
SortExpression="Name" />
<asp:TemplateField HeaderText="Machine Type"
SortExpression="MachineType">
<ItemTemplate>
<asp:Label ID="MachineTypeLabel"
runat="server"
Text='<%# Bind("MachineType") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="MachineTypeDropDown"
runat="server"
AppendDataBoundItems="True"
Height="21px"
Width="217px"
DataSourceID="GetMachineType"
DataTextField="Name"
DataValueField="ID">
<asp:ListItem Enabled="true"
Text="Select a Machine Type."
Value="NULL">
</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Machine Model" SortExpression="MachineModel">
<ItemTemplate>
<asp:Label ID="MachineModelLabel"
runat="server"
Text='<%# Bind("MachineModel") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="MachineModelDropDown"
runat="server"
AppendDataBoundItems="True"
Height="21px" Width="217px"
DataSourceID="GetMachineModel"
DataTextField="Name"
DataValueField="ID">
<asp:ListItem Enabled="true"
Text="Select a Machine Model."
Value="NULL">
</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button"
ShowEditButton="True"
CausesValidation="false" >
<ItemStyle HorizontalAlign="Center"
Wrap="True" />
</asp:CommandField>
</Columns>
非常感谢任何帮助或建议。
谢谢
答案 0 :(得分:3)
正如Etch所说,您可能需要搜索整个控制层次结构。然而,Etch的方法很脆弱。控件中的FindControl机制目前有点弱,因为它只检查直接控制子项 - 而不是层次结构。下面是一个实现,它将检查所有查找具有您想要的ID的控件。
/// <summary>
/// Similar to Control.FindControl, but recurses through child controls.
/// </summary>
public static T FindControl<T>(Control startingControl, string id) where T : Control
{
T found = startingControl.FindControl(id) as T;
if (found == null)
{
found = FindChildControl<T>(startingControl, id);
}
return found;
}
/// <summary>
/// Similar to Control.FindControl, but recurses through child controls.
/// Assumes that startingControl is NOT the control you are searching for.
/// </summary>
public static T FindChildControl<T>(Control startingControl, string id) where T : Control
{
T found = null;
foreach (Control activeControl in startingControl.Controls)
{
found = activeControl as T;
if (found == null || (string.Compare(id, found.ID, true) != 0))
{
found = FindChildControl<T>(activeControl, id);
}
if (found != null)
{
break;
}
}
return found;
}
因此,您可以使用gridview容器作为第一个参数,以及您要查找的子控件的ID来调用它。例如,如果您将这些方法实现到一个名为ControlHelper的类中(有更好的方法......)
DropDownList theList = ControlHelper.FindControl<DropDownList>(grdViewParentInstance, "MachineTypeDropDown");
if(theList != null) {
theList.Selected... ;
}
答案 1 :(得分:1)
您是否检查了控件集合中的所有控件?
我记得要做这样的事情:
machineModelid = ((DropDownList)(row.Cells[3].Controls[0].Controls[1])).SelectedValue;
控件可以嵌套在另一个控件中吗?我还会用“查看源”检查你的html标记,看看是否有什么看起来很奇怪或不合适。
我还建议发布一些标记。所以我们对我们正在处理的内容有了更多了解。