单选按钮更改值犯规更新hiddenfield? [英] Radiobutton changed value doesnt update hiddenfield?

查看:148
本文介绍了单选按钮更改值犯规更新hiddenfield?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个隐藏字段在一个asp.net的GridView像这样:

I've got a hidden field in an asp.net gridview like so:

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

隐藏字段用于因此,如果在我的GridView的另一个领域是改变了我可以用值1这种方式更新这个隐藏字段的值,当我点击保存按钮,我可以遍历的我行GridView和只叫我的数据库更新程序,当且仅当hiddenField.Value =1。

The hidden field is used so that if another field on my gridview is changed I can update the value of this hidden field with the value 1. That way when I click a "Save" button I can loop through the rows of my gridview and only call my database update routine if and only if the hiddenField.Value = "1".

我轻松地设置在GridView的的RowDataBound 情况下,本场像这样的一个文本框:

I set this field in the rowdatabound event of the gridview easily with something like this for a textbox:

    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");

这工作正常的文本框和下拉菜单,但我也有我的网格中的单选按钮列表。当我只需选择单选按钮,在我的gridview的改变,例如3行,它始终只更新1,出现的隐藏字段犯规得到正确设置。我以为我可以正确设置是这样的:

This works fine for textboxes and drop downs but I also have a radiobuttonlist in my grid. When I change for instance 3 rows in my gridview by just selecting the radio button, it always ONLY updates 1. It appears the hidden field doesnt get set correctly. I thought I could set it correctly like this:

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。

This doesn't appear to work and consider clicks for each radio button. As I said if I update three rows by just clicking the radiobutton in each row and then click my save button it only calls the update routine once. It appears the hidden field does not get the value 1.

谁能帮助?

以防万一,这里是我的整个网格视图控件:

Just in case here is my entire grid view control:

<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得到一个警告:

I even tried some jquery to get an alert:

  $(function () {
            $("#<%=gvLineItems.ClientID %> input[type=radio]").click(function () {
                alert("hi");
            });
        });

但我不认为这是正确的,因为当我点击一个单选按钮,我没有看到任何警报。我还需要更新域 hdnIsChanged 用值1 hiddenfield当从单选按钮列表单选按钮项 rbAnswer 是点击。我行这样做jQuery的,但可以使用一些帮助。该hiddenfield hdnIsChanged和RadioButtonList的rbAnswer都是一个asp.net GridView控件中。

But I dont think that is right because when I click a radio button I am not seeing any alert. I also need to update the field hdnIsChanged the hiddenfield with the value 1 when a radio button item from the radiobuttonlist rbAnswer is clicked. I am ok to do this in jquery but could use some help. The hiddenfield hdnIsChanged and the radiobuttonlist rbAnswer are both inside an asp.net gridview.

推荐答案

我终于得到它得益于通过在浏览器中谷歌的开发人员工具调试。
我最初注意到我的网页上在RadioButtonList的每一行中(右击检查元素)每个单选按钮有一个onclick事件attribute..Great至今。

I finally got it thanks to debugging through googles developers tool within the browser. I noticed initially on my page (right click inspect element) every radio button within the radiobuttonlist for each row had an "onclick" attribute..Great so far.

我在一个单选按钮点击的那一刻,我有的AutoPostBack =真正的我的RadioButtonList的,我就可以点击分钟,即有onclick属性是我点击该行的唯一项目。每隔一行属性就消失了。

The minute I clicked on a radiobutton, I have "AutoPostBack=true" for my radiobuttonlist, the minute I clicked on it, the only item that had the onclick attribute was for the row I clicked. Every other row the attribute was gone.

简单的解决方案是通过在GridView的每一行在RadioButtonList循环的的SelectedIndexChanged 事件,并重新进行添加的属性。

The simple solution was in the selectedindexchanged event of the radiobuttonlist to loop through each row in the gridview and readd the attribute.

 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");
               }
            }

这篇关于单选按钮更改值犯规更新hiddenfield?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆