我用GridView绑定了数据。我想要做编辑更新功能。
<asp:TemplateField HeaderText="对与错">
                    <EditItemTemplate>
                        <asp:RadioButton ID="rbtnYes" runat="server" Text="√" GroupName="button" />
                        <asp:RadioButton ID="rbtnNo" runat="server" Text="×" GroupName="button" />
                    </EditItemTemplate>
                    <ItemTemplate>
                    <%# Eval("Hj_YesOrNo")%>
                    </ItemTemplate>
                </asp:TemplateField>RadioButton一进来的时候是根据数据库的值先绑定的。也就是说。数据库要是√。那rbtnYes应该默认选中。。我怎么获取EditItemTemplate里面的控件。然后进行绑定呀?我用for能只能拿出ItemTemplate里面的控件。因为RadioButton 放在EditItemTemplate中。所以不知道怎么拿if (e.Row.RowState == DataControlRowState.Edit)
        {
            Label lblID = e.Row.FindControl("lblID") as Label;
            RadioButton rbtnYes = e.Row.FindControl("rbtnYes") as RadioButton;
            RadioButton rbtnNo = e.Row.FindControl("rbtnNo") as RadioButton;
            HappyReadJudge Judge = HappyReadJudgeBLL.GetJudgeByID(Convert.ToInt32(lblID.Text));
            if (Judge.Hj_YesOrNo == "√")
            {
                rbtnYes.Checked = true;
            }
            else
            {
                rbtnNo.Checked = true;
            }
        }
我用了RowDataBound事件拿出数据。。可以。。但是在交替行的时候。就无法绑定了。也就是说。点第一行的时候。会进来绑定数据。但是点第二行的时候。因为是交替行。所以就不会进来。既然写了交替行。里面找出的控件也是为null 的有什么办法能把EditItemTemplate里面的控件拿出来啊?在RowUpdating里面就可以拿。但是在RowUpdating事件外怎么拿呢?会的来帮一下!!

解决方案 »

  1.   

    你是想设置初期值吗?在rowcreate中应该可以设置的。
      

  2.   

     <EditItemTemplate>
      <asp:RadioButton ID="rbtnYes" runat="server" Text="√" GroupName="button" />
      <asp:RadioButton ID="rbtnNo" runat="server" Text="×" GroupName="button" />
      </EditItemTemplate>
    <ItemTemplate>
      <asp:RadioButton ID="rbtnYes2" runat="server" Text="√" GroupName="button" />
      <asp:RadioButton ID="rbtnNo2" runat="server" Text="×" GroupName="button" />
      <%# Eval("Hj_YesOrNo")%>
      </ItemTemplate> if (e.Row.RowState == DataControlRowState.Edit || ((int)(e.Row.RowState & DataControlRowState.Edit)) != 0)
                    {
                        RadioButton  rdbl= e.Row.FindControl("rbtnYes") as RadioButton ;
                      //查询
                    }
      

  3.   

    if (e.Row.RowState == DataControlRowState.Edit)
    e.Row.RowState   ==   (DataControlRowState.Alternate|DataControlRowState.Edit) 
    详细请看
    http://blog.xunbin.com/Article/2010/9/2/187/
      

  4.   

    完整的例子、<%@ Page Language="C#" AutoEventWireup="true" Debug="true" %>
    <%@ Import Namespace="System.Data" %>
    <script runat="server">
    string GetUserPhoto(object pathPhoto)
    {
      if (pathPhoto == DBNull.Value)
      {
        return "<img src='../Images/none.gif'>";
      }
      else
      {
        return "<img src='../Upload/" + pathPhoto.ToString() + "'>";
      }
    }protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
        if (e.Row.RowIndex == GridView1.EditIndex)
        {      DataRowView rowItem = (DataRowView)e.Row.DataItem;      DropDownList clsName = (DropDownList)e.Row.FindControl("uClassName");
          if (rowItem["ClassName"] != DBNull.Value)
          {
            clsName.Items.FindByText(rowItem["ClassName"].ToString()).Selected = true;
          }      if (rowItem["Gender"] != DBNull.Value)
          {        RadioButtonList oGender = (RadioButtonList)e.Row.FindControl("uGender");
            oGender.SelectedIndex = (Convert.ToBoolean(rowItem["Gender"]) ? 0 : 1);
          }
        }
      }
    }protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
      if (e.CommandName == "New")
      {
        string StudentTitle = ((TextBox)GridView1.FooterRow.FindControl("NewTitle")).Text;
        if (StudentTitle.Trim() == "")
        {
          ErrorMsg.Text = "请输入姓名";
          return;
        }
        string StudentBirthDay = ((TextBox)GridView1.FooterRow.FindControl("NewBirthDay")).Text;
        bool StudentGender = ((RadioButtonList)
          GridView1.FooterRow.FindControl("NewGender")).SelectedValue == "男" ? true : false;
        string StudentClassName = ((DropDownList)
          GridView1.FooterRow.FindControl("NewClassName")).SelectedValue;
        FileUpload oUpload = (FileUpload)GridView1.FooterRow.FindControl("AddPhoto");
        String FileName = "";
        FileName = Guid.NewGuid().ToString("D") + System.IO.Path.GetExtension(oUpload.FileName);
        oUpload.SaveAs(Server.MapPath("~") + "/Upload/" + FileName);
        string sql = "Insert Into Student (Title,BirthDay,Gender,PhotoPath,ClassName)";
        sql += " Values(@Title,@BirthDay,@Gender,@PhotoPath,@ClassName)";
        SqlDataSource1.InsertCommand = sql;
        SqlDataSource1.InsertParameters.Add("@Title", TypeCode.String, StudentTitle);
        SqlDataSource1.InsertParameters.Add("@BirthDay", TypeCode.DateTime, StudentBirthDay);
        SqlDataSource1.InsertParameters.Add("@Gender", TypeCode.Boolean, StudentGender.ToString());
        SqlDataSource1.InsertParameters.Add("@PhotoPath", TypeCode.String, FileName);
        SqlDataSource1.InsertParameters.Add("@ClassName", TypeCode.String, StudentClassName);
        SqlDataSource1.Insert();
      }  if (e.CommandName == "Update")
      {
        string StudentTitle = ((TextBox)GridView1.Rows[GridView1.EditIndex].FindControl("uTitle")).Text;
        string StudentBirthDay = ((TextBox)
          GridView1.Rows[GridView1.EditIndex].FindControl("uBirthDay")).Text;
        bool StudentGender = ((RadioButtonList)
          GridView1.Rows[GridView1.EditIndex].FindControl("uGender")).SelectedValue == "男" ? true : false;
        string StudentClassName = ((DropDownList)
          GridView1.Rows[GridView1.EditIndex].FindControl("uClassName")).SelectedValue;
        string StudentID = GridView1.DataKeys[GridView1.EditIndex].Value.ToString();
        String FileName = "";
        string sql = "";
        String PhotoPath = "";
        bool HasFileUploaded = false;    FileUpload oUpload = (FileUpload)GridView1.Rows[GridView1.EditIndex].FindControl("uPhoto");
        if (oUpload.HasFile)
        {
          PhotoPath = Guid.NewGuid().ToString("D") + System.IO.Path.GetExtension(oUpload.FileName);
          oUpload.SaveAs(Server.MapPath("~") + "/Upload/" + PhotoPath);
          HasFileUploaded = true;
        }    if (HasFileUploaded)
        {
          sql = "Update Student Set Title=@Title,BirthDay = @BirthDay,";
          sql += "Gender=@Gender,PhotoPath=@PhotoPath,ClassName=@ClassName Where id=@id";
        }
        else
        {
          sql = "Update Student Set Title=@Title,BirthDay = @BirthDay," ;
          sql += "Gender=@Gender,ClassName=@ClassName Where id=@id";
        }
        SqlDataSource1.UpdateCommand = sql;
        SqlDataSource1.UpdateCommandType = SqlDataSourceCommandType.Text;    SqlDataSource1.UpdateParameters.Add("@Title", TypeCode.String, StudentTitle);
        SqlDataSource1.UpdateParameters.Add("@BirthDay", TypeCode.DateTime, StudentBirthDay);
        SqlDataSource1.UpdateParameters.Add("@Gender", TypeCode.Boolean, StudentGender.ToString());
        if (HasFileUploaded)
        {
          SqlDataSource1.UpdateParameters.Add("@PhotoPath", TypeCode.String, PhotoPath);
        }
        SqlDataSource1.UpdateParameters.Add("@ClassName", TypeCode.String, StudentClassName);
        SqlDataSource1.UpdateParameters.Add("@id", TypeCode.Int32, StudentID);    SqlDataSource1.Update();
      }
    }  
    </script><html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>GridView 插入、删除、修改的例子</title>
    </head>
    <body>
    <form id="form1" runat="server">
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
        AllowPaging="true" AllowSorting="true" PageSize="6" DataSourceID="SqlDataSource1"
        OnRowDataBound="GridView1_RowDataBound" ShowFooter="true"
        OnRowCommand="GridView1_RowCommand">
        <Columns>
          <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
            Visible="false" SortExpression="id" />
          <asp:TemplateField HeaderText="姓名" SortExpression="Title">
            <ItemTemplate>
              <%#Eval("Title") %>
            </ItemTemplate>
            <EditItemTemplate>
              <asp:TextBox ID="uTitle" runat="server" Text='<%#Eval("Title") %>'></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
              <asp:TextBox ID="NewTitle" runat="server"></asp:TextBox>
            </FooterTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="生日" SortExpression="BirthDay">
            <ItemTemplate>
              <%#Eval("BirthDay", "{0:yyyy年M月d日}")%>
            </ItemTemplate>
            <EditItemTemplate>
              <asp:TextBox ID="uBirthDay" runat="server" Text='<%#Eval("BirthDay", "{0:yyyy-M-d}")%> '/>
            </EditItemTemplate>
            <FooterTemplate>
              <asp:TextBox ID="NewBirthDay" runat="server"></asp:TextBox>
            </FooterTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="性别" SortExpression="Gender">
            <ItemTemplate>
              <%#(Eval("Gender")).ToString() =="True"?"男":"女"%>
            </ItemTemplate>
            <EditItemTemplate>
              <asp:Label ID="g" Text='<%#(Eval("Gender")).ToString() =="True"?"男":"女"%>'
                Visible="false" runat="server"></asp:Label>
              <asp:RadioButtonList ID="uGender" runat="server" RepeatDirection="Horizontal">
                <asp:ListItem Text="男"></asp:ListItem>
                <asp:ListItem Text="女"></asp:ListItem>
              </asp:RadioButtonList>
            </EditItemTemplate>
            <FooterTemplate>
              <asp:RadioButtonList ID="NewGender" runat="server" RepeatDirection="Horizontal">
                <asp:ListItem Selected="true" Text="男"></asp:ListItem>
                <asp:ListItem Text="女"></asp:ListItem>
              </asp:RadioButtonList>
            </FooterTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="年级">
            <ItemTemplate>
              <%#Eval("ClassName")%>
            </ItemTemplate>
            <EditItemTemplate>
              <asp:DropDownList ID="uClassName" runat="server">
                <asp:ListItem Text="小学" Value="小学"></asp:ListItem>
                <asp:ListItem Text="中学" Value="中学"></asp:ListItem>
                <asp:ListItem Text="高中" Value="高中"></asp:ListItem>
                <asp:ListItem Text="大学" Value="大学"></asp:ListItem>
              </asp:DropDownList>
            </EditItemTemplate>
            <FooterTemplate>
              <asp:DropDownList ID="NewClassName" runat="server">
                <asp:ListItem Text="小学" Value="小学"></asp:ListItem>
                <asp:ListItem Text="中学" Value="中学"></asp:ListItem>
                <asp:ListItem Text="高中" Value="高中"></asp:ListItem>
                <asp:ListItem Text="大学" Value="大学"></asp:ListItem>
              </asp:DropDownList>
            </FooterTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="照片">
            <ItemTemplate>
              <%# GetUserPhoto(Eval("PhotoPath")) %>
            </ItemTemplate>
            <EditItemTemplate>
              <asp:FileUpload ID="uPhoto" runat="server" />
            </EditItemTemplate>
            <FooterTemplate>
              <asp:FileUpload ID="AddPhoto" runat="server" />
            </FooterTemplate>
          </asp:TemplateField>
          <asp:CommandField ButtonType="Button" ShowCancelButton="true" ShowDeleteButton="true"
            ShowEditButton="true" CancelText="取消" DeleteText="删除" UpdateText="更新" 
            EditText="修改" HeaderText="操作" InsertVisible="false" 
            ShowInsertButton="true" NewText="添加学生" />
        </Columns>
      </asp:GridView>
      <asp:SqlDataSource ID="SqlDataSource1" runat="server" ProviderName="System.Data.OleDb"
        ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\ASPNET20Book.mdb;"
        SelectCommand="SELECT * FROM [Student] ORDER BY [id] DESC" DataSourceMode="DataSet"
        DeleteCommand="Delete from Student Where id=@id">
        <DeleteParameters>
          <asp:Parameter Name="id" Type="Int32" />
        </DeleteParameters>
      </asp:SqlDataSource>
      <asp:Label ID="ErrorMsg" runat="server" ForeColor="red"></asp:Label>
    </form>
    </body>
    </html>
      

  5.   

    另外一种完整的实现方法http://dotnet.aspx.cc/file/Add-Delete-Update-Edit-Data-With-Paging-in-GridView.aspx
      

  6.   

    for (int row = 0; row < GvUpdateApply.Rows.Count; row++)
                {
                    TextBox tb = (TextBox)GvUpdateApply.Rows[row].Cells[0].FindControl("txtcardid");
                    。
                }这样,不管你怎么取控件都取的出来
      

  7.   

    自己搞定啦。后台弄2个方法就OK了。一个放回false一个返回true。。然后Checked='<%# 方法(Eval("XXX")) %>'   这方法可能比较笨。不过效果还是做出来了。还是谢谢各位啦
      

  8.   


    如果行的话我也不用来问了。这个只能取出Item里面的控件。Edit里面的取不出来的
      

  9.   


    呵呵。我已经说啦。不用事件的。用事件的话RowUpdating事件是可以拿到的。