【发布时间】:2013-08-07 04:57:30
【问题描述】:
我在 asp.net gridview 中有一个隐藏字段,如下所示:
<asp:TemplateField>
<ItemTemplate>
<input type="hidden" value="0" id="hdnIsChanged" runat="server" />
</ItemTemplate>
</asp:TemplateField>
使用隐藏字段,以便如果我的 gridview 上的另一个字段发生更改,我可以使用值 1 更新此隐藏字段的值。这样,当我单击“保存”按钮时,我可以遍历我的行gridview 并且仅当且仅当 hiddenField.Value = "1" 时才调用我的数据库更新例程。
我在 gridview 的 rowdatabound 事件中轻松设置了这个字段,使用类似这样的文本框:
HtmlInputHidden hiddenField = (HtmlInputHidden)e.Row.FindControl("hdnIsChanged"); //get handle on hidden row isDirty
TextBox notes = (TextBox)e.Row.FindControl("txtNotes"); //get notes
//assign onchange / onclick event handlers when something is changed set hidden field to 1
notes.Attributes.Add("onchange", "document.getElementById('" + hiddenField.ClientID + "').value=1");
这适用于文本框和下拉菜单,但我的网格中也有一个单选按钮列表。例如,当我通过选择单选按钮更改网格视图中的 3 行时,它总是只更新 1。似乎隐藏字段没有正确设置。我想我可以像这样正确设置它:
RadioButtonList rbl = (RadioButtonList)e.Row.FindControl("rbAnswer"); //get answer
//assign onchange / onclick event handlers when something is changed set hidden field to 1
foreach(ListItem li in rbl.Items)
li.Attributes.Add("onclick", "document.getElementById('" + hiddenField.ClientID + "').value=1");
这似乎不起作用并考虑每个单选按钮的点击次数。正如我所说,如果我通过单击每行中的单选按钮更新三行,然后单击我的保存按钮,它只会调用一次更新例程。看来隐藏字段没有得到值 1。
谁能帮忙?
编辑
以防万一这是我的整个网格视图控件:
<asp:UpdatePanel ID="myPanel" runat="server" UpdateMode="Always" >
<ContentTemplate>
<asp:GridView ID="gvLineItems" runat="server" AllowSorting="True"
AlternatingRowStyle-BackColor="#e5f1fa" AutoGenerateColumns="False"
BackColor="#E8E8E8" CellPadding="4" DataKeyNames="ID" Font-Size="Small"
GridLines="Horizontal" HeaderStyle-BackColor="#4B6C9E"
HeaderStyle-ForeColor="White" ShowFooter="True" Width="700px"
onrowdatabound="gvLineItems_RowDataBound"
onrowcreated="gvLineItems_RowCreated">
<AlternatingRowStyle BackColor="#E5F1FA" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<input type="hidden" value="0" id="hdnIsChanged" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="HeaderLevelCheckBox" runat="server"
onclick="toggleSelection(this);" ToolTip="Select / Deselect all rows?" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelector" runat="server" onclick="ChangeRowColor(this)"
ToolTip="Select row?" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="1%" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField Visible="False">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" style="display:none"
Text='<%# DataBinder.Eval(Container, "DataItem.ID") %>' Width=".05px"></asp:Label>
</ItemTemplate>
<ItemStyle Width="1%" />
<ControlStyle Width="0px" />
<HeaderStyle Width="0px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="#">
<ItemTemplate>
<asp:Label ID="lblSortOrder" runat="server"
Text='<%# DataBinder.Eval(Container, "DataItem.SortOrder") %>'></asp:Label>
</ItemTemplate>
<FooterStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Validation">
<ItemTemplate>
<asp:Label ID="lblValidationItem" runat="server" ToolTip="Type of validation."
Text='<%# DataBinder.Eval(Container, "DataItem.ValidationItem") %>'></asp:Label>
</ItemTemplate>
<FooterStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Test">
<ItemTemplate>
<asp:Label ID="lblTest" runat="server" ToolTip="Type of test."
Text='<%# DataBinder.Eval(Container, "DataItem.Test") %>'></asp:Label>
</ItemTemplate>
<FooterStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Method">
<ItemTemplate>
<table>
<tbody>
<tr>
<td> <asp:Label ID="lblMethod" Font-Bold="true" runat="server" ToolTip="Method / Question"
Text='<%# DataBinder.Eval(Container, "DataItem.Method") %>'></asp:Label></td>
</tr>
<tr>
<td>
<asp:Label ID="lblMethodNotes" CssClass="tiny" runat="server" ToolTip="Specifications / Notes"
Text='<%# DataBinder.Eval(Container, "DataItem.MethodNotes") %>'></asp:Label>
</td>
</tr>
</tbody>
</table>
</ItemTemplate>
<ControlStyle Width="250px" />
<FooterStyle HorizontalAlign="Left" />
<HeaderStyle HorizontalAlign="Left" Width="250px" />
<ItemStyle HorizontalAlign="Left" Width="250px" Wrap="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Answer">
<ItemTemplate>
<div id="dMainAnswer">
<asp:RadioButtonList ToolTip="Please provide an answer to the method." AutoPostBack="true" RepeatDirection="Horizontal" ID="rbAnswer" runat="server" SelectedValue='<%# DataBinder.Eval(Container, "DataItem.AnswerID")%>' OnSelectedIndexChanged="rbAnswer_SelectedIndexChanged">
<asp:ListItem Text="Yes" Value="Yes" style="color:green;"></asp:ListItem>
<asp:ListItem Text="No" Value="No" style="color:red;"></asp:ListItem>
<asp:ListItem Text="N/A" Value="N/A"></asp:ListItem>
<asp:ListItem Value="" Text="" style="display: none" />
</asp:RadioButtonList>
<asp:Panel ID="pnlAnswer" runat="server" Visible="false">
<div id="dMainAnswerResponsibleType">
<asp:RadioButtonList ID="rbRespType" ToolTip="Select responsible contact type." runat="server" RepeatDirection="Horizontal" AutoPostBack="true" SelectedValue='<%# DataBinder.Eval(Container, "DataItem.ResponsiblePartyType")%>' OnSelectedIndexChanged="rbRespType_SelectedIndexChanged">
<asp:ListItem Selected="True" Text="SE" Value="SE">SE</asp:ListItem>
<asp:ListItem Text="Other" Value="Other">Other</asp:ListItem>
<asp:ListItem Value="" Text="" style="display: none" />
</asp:RadioButtonList>
</div>
<div id="dMainAnswerResponsible">
<asp:DropDownList ID="ddlEmployees" runat="server"
DataSource="<%# GetEmployees() %>" SelectedValue='<%# Eval("SEContact") %>' DataTextField="FullName" Width="75px"
DataValueField="FullName"
ToolTip="Select the SE responsible party.">
</asp:DropDownList>
<asp:TextBox ID="txtContact" Text='<%# Eval("ResponsiblePartyContact") %>' Width="75px" MaxLength="50" runat="server" ToolTip="Enter the responsible contact name." Visible="false"></asp:TextBox>
</div>
</asp:Panel>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Notes">
<ItemTemplate>
<asp:TextBox MaxLength="5000" runat="server" ToolTip="Enter any additional notes." ID="txtNotes" TextMode="MultiLine" Text='<%# DataBinder.Eval(Container, "DataItem.Notes") %>'></asp:TextBox>
</ItemTemplate>
<ControlStyle Font-Names="Arial" />
<FooterStyle HorizontalAlign="Left" />
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#4B6C9E" ForeColor="White" />
</asp:GridView>
<div style="width:100%;text-align:center;">
<asp:Label ID="lblLineItemMessage" runat="server"></asp:Label>
<asp:UpdateProgress AssociatedUpdatePanelID="myPanel" DisplayAfter="1" ID="udProgress" runat="server" Visible="True" DynamicLayout="True">
<ProgressTemplate>
<img style="border-style:none;" src="images/ajax-loader.gif" alt="loading" />
</ProgressTemplate>
</asp:UpdateProgress>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlTypes" />
<asp:AsyncPostBackTrigger ControlID="ddlValidations" />
<asp:AsyncPostBackTrigger ControlID="ddlTests" />
<asp:AsyncPostBackTrigger ControlID="ddlJobs" />
<asp:AsyncPostBackTrigger ControlID="rbMethod" />
<asp:AsyncPostBackTrigger ControlID="TreeView1" />
</Triggers>
</asp:UpdatePanel>
我什至尝试了一些 jquery 来获得警报:
$(function () {
$("#<%=gvLineItems.ClientID %> input[type=radio]").click(function () {
alert("hi");
});
});
但我认为这是不对的,因为当我单击单选按钮时,我没有看到任何警报。当单击单选按钮列表rbAnswer 中的单选按钮项时,我还需要将隐藏字段hdnIsChanged 的值更新为1。我可以在 jquery 中执行此操作,但可以使用一些帮助。 hiddenfield hdnIsChanged 和 radiobuttonlist rbAnswer 都在 asp.net gridview 中。
【问题讨论】:
-
当你最需要他的时候@Tim Schmelter 在哪里 :)。
-
并且更新的行是您单击的最后一个单选按钮的行?
-
@RoboLover - 没有更新的行是我点击的第一个。如果我有 20 行并单击 1-19。然后我点击保存按钮。仅更新第 1 行。我已经调试了我的代码,它显示 hdnIsChanged 隐藏字段的值为 1 仅适用于第 1 行。
-
您确定所有单选按钮在网页上都以不同的 id 呈现吗?
-
@RoboLover - 是的,它们在网格视图中,例如这里是一个:
//*[@id="MainContent_gvLineItems_rbAnswer_1_0_1"],另一个可能是 ..._1_0_2,另一个可能是 ...2_0_1 等等..每个有一个唯一的 ID。
标签: c# javascript asp.net