在asp.net中的按钮点击事件中,RadioButton checked属性始终为false

时间:2014-09-30 10:18:12

标签: javascript c# asp.net gridview

这是一个应用程序,从下拉列表中选择适当的选项相应的策略 显示在GridView中。此GridView有一列RadioButtons,选中后可以更新特定策略。使用TemplateField添加RaidioButtons列。 我面临的问题是,当我选择RadioButton并单击Update Button时,Checked属性中的值始终为“false”(显示在文本框中)。前端代码(aspx文件)如下: -

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SimpleForm.aspx.cs"  Inherits="Test.SimpleForm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">

<script language="javascript" type="text/javascript">
   function SelectSingleRadiobutton(rdbtnid) {
      var rdBtn = document.getElementById(rdbtnid);
    var rdBtnList = document.getElementsByTagName("input");
    for (i = 0; i < rdBtnList.length; i++) {
        if (rdBtnList[i].type == "radio" && rdBtnList[i].id != rdBtn.id) {
            rdBtnList[i].checked = false;
        }

    }
 }
</script>
<title></title>

<style type="text/css">
   body 
   {
    background-color:White;
}
#form1{
text-align: center;
/*width: 500px;*/
background: white;
margin: 0px auto;
padding: 5px;
font-family: verdana,geneva,sans-serif;
font-size:13px;
} 

#form1 .label {
text-align: right;
display: block;
margin: 10px 0px 10px 10px;
width:40%;
}

#form1 .input{
text-align: left;
display: block;
margin: 10px 0 10px 10px;
width:40%;
margin-left:10px;
}
</style>
</head>
<body>
 <form id="form1" runat="server">
  <fieldset style=" border:0px;">
    <div class="row" style=" width:100%;">
      <div class="label"  style=" width:100% ; text-align:center; font-size:20px;">
        <asp:Label ID="Label" runat="server" 
            Text="Backup Policies"></asp:Label>
       </div>

     </div>


 <div class="row" style=" width:100%;">
    <div class="label" style="float:left;">
        <asp:Label ID="Label2" runat="server" Text="Application Name :"></asp:Label>
    </div>
    <div class="input" style=" float:left; margin-left:20px; width:20%">
        <asp:DropDownList ID="AppNameDropDown" runat="server" Height="28px" Width="250px" 
            DataSourceID="AppNameDataSource" DataTextField="AppName" DataValueField="AppName">
            <asp:ListItem Text="Dropdown1"></asp:ListItem>
            <asp:ListItem Text="Dropdown2"></asp:ListItem>
            <asp:ListItem Text="Dropdown3"></asp:ListItem>
        </asp:DropDownList>
        <asp:SqlDataSource ID="AppNameDataSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:new_inventoryConnectionString %>" 
            SelectCommand="SELECT AppName, AppID FROM tblApplication ORDER BY AppName">
        </asp:SqlDataSource>
    </div>
  <div id="PolicyButton" style=" float:left; margin: 10px 0 10px 10px;">
      <asp:Button ID="ShowPolicyButton" runat="server" Text="Show Policies" 
          Width="89px" onclick="ShowPolicyButton_Click" /></div>
  </div>

  <div id ="UpdateAdd" style=" margin: 40px 0 10px 10px;">
  <asp:Button ID="UpdateButton" runat="server" Text="Update" 
          style =" margin-right: 40px;" onclick="UpdateButton_Click" Visible="False"/>

  <asp:Button ID="AddButton" runat="server" Text="Add" Visible="False" />
  </div>

  <div id="view" style=" margin-top:30px">

      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
          DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" 
          GridLines="None">
          <AlternatingRowStyle BackColor="White" />
          <Columns>

          <asp:TemplateField>
          <ItemTemplate>


          <asp:RadioButton ID = "chkid" runat = "server" GroupName="policy" 
          OnClick="javascript:SelectSingleRadiobutton(this.id); "  
          Visible="True" />


          </ItemTemplate>
           </asp:TemplateField>              
              <asp:BoundField DataField="SrvIP" HeaderText="SrvIP" SortExpression="SrvIP" />
              <asp:BoundField DataField="PolicyName" HeaderText="PolicyName" 
                  SortExpression="PolicyName" />
              <asp:BoundField DataField="DCCall" HeaderText="DCCall" 
                  SortExpression="DCCall" />
             <asp:BoundField DataField="Freq" HeaderText="Freq" SortExpression="Freq" />
             <asp:BoundField DataField="Time" HeaderText="Time" SortExpression="Time" />
              <asp:BoundField DataField="App" HeaderText="App" SortExpression="App" />
              <asp:BoundField DataField="DB" HeaderText="DB" SortExpression="DB" />
              <asp:BoundField DataField="Web" HeaderText="Web" SortExpression="Web" />
              <asp:BoundField DataField="FS" HeaderText="FS" SortExpression="FS" />
              <asp:CheckBoxField />
          </Columns>
          <EditRowStyle BackColor="#2461BF" />
          <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
          <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
          <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
          <RowStyle BackColor="#EFF3FB" />
          <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
          <SortedAscendingCellStyle BackColor="#F5F7FB" />
          <SortedAscendingHeaderStyle BackColor="#6D95E1" />
          <SortedDescendingCellStyle BackColor="#E9EBEF" />
          <SortedDescendingHeaderStyle BackColor="#4870BE" />
      </asp:GridView>
      <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
          ConnectionString="<%$ ConnectionStrings:new_inventoryConnectionString %>" 
          SelectCommand="SELECT [SrvIP], [PolicyName], [DCCall], [Freq], [Time],[App] ,[DB] ,[Web] , [FS] FROM [View_BackupPolicy] WHERE ([AppName] LIKE '%' + @AppName + '%')">
          <SelectParameters>
              <asp:FormParameter FormField="AppNameDropDown" Name="AppName" Type="String" />
          </SelectParameters>
      </asp:SqlDataSource>
      <br />

    </div>




</fieldset>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
<p>
    &nbsp;</p>
</body>
</html>

这是背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


namespace Test
{
  public partial class SimpleForm : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.Visible = false;
    }

    protected void ShowPolicyButton_Click(object sender, EventArgs e)
    {


            GridView1.Visible = true;

            UpdateButton.Visible = true;
            AddButton.Visible = true;

    }



    protected void UpdateButton_Click(object sender, EventArgs e)
    {


            foreach (GridViewRow row in GridView1.Rows)
            {
                RadioButton chkid = (RadioButton)row.FindControl("chkid");
                String check = chkid.ToString();

                bool i = chkid.Checked;
                //TextBox1.Text = check;
                TextBox1.Text = i.ToString();

            }

   /*if (chkid.Checked)
    {
        string s = chkid.Text;
    TextBox1.Text =s;
    }
    else
    {
    TextBox1.Text ="false";
    }*/

  }
}
    }

我读了几个关于这个问题的问题,但他们都在Page_Load函数中完成了这个问题,因此使用IsPostBack解决了他们的问题。

有人能告诉我为什么会出现这个问题。

0 个答案:

没有答案