单选按钮更改的值不会更新隐藏字段

Radiobutton changed value doesnt update hiddenfield?

本文关键字:更新 隐藏 字段 单选按钮      更新时间:2023-09-26

我在asp.net网格视图中有一个隐藏字段,如下所示:

 <asp:TemplateField>
      <ItemTemplate>
          <input type="hidden" value="0" id="hdnIsChanged" runat="server" />
      </ItemTemplate>
 </asp:TemplateField>

使用隐藏字段是为了如果我的网格视图上的另一个字段发生更改,我可以用值1更新这个隐藏字段的值。这样,当我点击"保存"按钮时,我可以循环浏览我的网格视图的行,并且只在hiddenField时调用我的数据库更新例程。值="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中这样做,但可能需要一些帮助。隐藏字段hdnIsChanged和单选按钮列表rbAnswer都在asp.net网格视图中。

由于在浏览器中通过谷歌开发者工具进行了调试,我终于得到了它。我最初注意到,在我的页面上(右键单击检查元素(,每行的单选按钮列表中的每个单选按钮都有一个"onclick"属性。。到目前为止很棒。

单击单选按钮的那一分钟,我的单选按钮列表就有"AutoPostBack=true",单击它的那一刻,唯一具有onclick属性的项目就是我单击的行。每隔一行属性就不见了。

简单的解决方案是在单选按钮列表的selectedindexchanged事件中循环遍历网格视图中的每一行并读取属性。

 foreach (GridViewRow row in gvLineItems.Rows)
            {
               HtmlInputHidden hiddenField = (HtmlInputHidden)row.FindControl("hdnIsChanged");
               RadioButtonList rbl2 = (RadioButtonList)row.FindControl("rbAnswer");
              foreach (ListItem li in rbl2.Items)
               {
                   li.Attributes.Add("onclick", "document.getElementById('" + hiddenField.ClientID + "').value=1");
               }
            }

您是否考虑过使用jQuery?

<asp:GridView ID="grdViewLines" runat="server" CssClass="dirty-tracking-grid">
<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <input type="hidden" value="0" runat="server" row-id='<%# DataBinder.Eval(Container, "DataItem") %>'  />
        </ItemTemplate>
        <ItemTemplate>
            <asp:RadioButtonList runat="server" row-id='<%# DataBinder.Eval(Container, "DataItem") %>' CssClass="dirty-tracking">
                <asp:ListItem runat="server" Text="Item1"></asp:ListItem>
                <asp:ListItem runat="server" Text="Item2"></asp:ListItem>
            </asp:RadioButtonList>                    
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField></asp:TemplateField>
</Columns>

<script type="text/javascript">
$(function () {
    $(".dirty-tracking :radio").click(function () {
        var rowId = $(this).closest("table").attr("row-id");
        $(".dirty-tracking-grid input[type=hidden,row-id=" + rowId).val(1);
    });
});