次のように、asp.net グリッドビューに隠しフィールドがあります。
<asp:TemplateField>
<ItemTemplate>
<input type="hidden" value="0" id="hdnIsChanged" runat="server" />
</ItemTemplate>
</asp:TemplateField>
非表示フィールドを使用して、グリッドビューの別のフィールドが変更された場合に、この非表示フィールドの値を値 1 で更新できるようにします。そのようにして、[保存] ボタンをクリックすると、グリッドビューの行をループして、 hiddenField.Value = "1" の場合にのみ、データベース更新ルーチンを呼び出します。
グリッドビューのイベントでこのフィールドを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");
これは機能していないようで、各ラジオボタンのクリックを考慮しています。前述のように、各行のラジオボタンをクリックして 3 つの行を更新し、保存ボタンをクリックすると、更新ルーチンが 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");
});
});
しかし、ラジオボタンをクリックしてもアラートが表示されないため、それは正しいとは思いません。hdnIsChanged
また、radiobuttonlist のラジオ ボタン項目がクリックされたときに、hiddenfieldフィールドを値 1 で更新する必要がrbAnswer
あります。jqueryでこれを実行しても問題ありませんが、助けが必要です。hiddenfield hdnIsChanged と radiobuttonlist rbAnswer は両方とも asp.net グリッドビュー内にあります。