2013-11-24 57 views
0

我想從後面的代碼中的數據表中填充gridview。在其中一列中生成動態複選框。這是GridView控件的代碼:checkbox_oncheckedchange事件不在gridview中觸發

<asp:GridView ID="grdAdDetails" AutoGenerateColumns="False" runat="server"> 
         <Columns> 
          <asp:BoundField HeaderText="Ad Id" DataField="Ad Id" Visible="false"></asp:BoundField> 
          <asp:BoundField HeaderText="Ad Type" DataField="Ad Type" /> 
          <asp:TemplateField HeaderText="Ad"> 
          <ItemTemplate> 
          <img src='<%#Eval("Ad") %>' height="150px" width="150px" /> 
          </ItemTemplate> 
          </asp:TemplateField> 
          <asp:BoundField HeaderText="Ad Url" DataField="Ad Url" /> 
          <asp:TemplateField HeaderText="Active"> 
          <ItemTemplate> 
          **<asp:CheckBox ID="chkAds" Checked='<%#((bool)Eval("Active"))%>' runat="server" OnCheckedChanged="chkAds_OnCheckedChanged" />** 
          </ItemTemplate> 
          </asp:TemplateField> 
          <%--<asp:BoundField HeaderText="Active" DataField="Active" />--%> 
          <asp:BoundField HeaderText="Node Id" DataField="Node Id" /> 
         </Columns> 
        </asp:GridView> 

,這是從後面的代碼數據表代碼:

DataTable dt = new DataTable(); 
      dt.Columns.Add("Ad Id", typeof(string)); 
      dt.Columns.Add("Ad Type", typeof(string)); 
      dt.Columns.Add("Ad", typeof(string)); 
      dt.Columns.Add("Ad Url", typeof(string)); 
      dt.Columns.Add("Active", typeof(bool)); 
      dt.Columns.Add("Node Id", typeof(string)); 
      DataRow dr = dt.NewRow(); 
      con.Open(); 
      SqlCommand cmd = new SqlCommand("[temp].[somename]", con); 
      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.Parameters.AddWithValue("@nodeId", 1088); 
      cmd.Parameters.AddWithValue("@adType", 0); 
      using (var reader = cmd.ExecuteReader()) 
      { 
       while (reader.Read()) 
       { 
        dr["Ad Id"] = reader[0].ToString(); 
        dr["Ad Type"] = reader[1].ToString(); 
        dr["Ad"] = reader[2].ToString(); 
        dr["Ad Url"] = reader[3].ToString(); 
        dr["Active"] =Convert.ToBoolean(reader[4]); 
        dr["Node Id"] = reader[5].ToString(); 

       } 
       dt.Rows.Add(dr); 
      } 
      con.Close(); 
      grdAdDetails.DataSource = dt; 
      grdAdDetails.DataBind(); 

複選框調用onChanged代碼:

public void chkAds_OnCheckedChanged(object sender, EventArgs e) 
     { 
      int selRowIndex = ((GridViewRow)(((CheckBox)sender).Parent.Parent)).RowIndex; 
      CheckBox cb = (CheckBox)grdAdDetails.Rows[selRowIndex].FindControl("chkAds"); 

      if (cb.Checked) 
      { 
       //some code here 
      } 
     } 

當我檢查/取消選中該複選框,OnCheckedChanged事件未觸發。任何人都可以幫忙嗎?

+2

如果我是正確的,你應該添加到複選框屬性下面的AutoPostBack = 「真」。 – Christos

+0

最初我添加了它......但只有頁面一次又一次地重新加載,但仍然沒有打到後面的代碼 – nitinvertigo

回答

0

我已經在這裏添加了一個樣本plz chk它。

觀點:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" AutoGenerateColumns="false" 
          OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" 
          OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"> 
          <Columns> 
           <asp:TemplateField> 
            <ItemTemplate> 
             <asp:checkbox id="CheckBox1" runat="server" AutoPostBack="true" oncheckedchanged="CheckBox1_CheckedChanged" /> 
            </ItemTemplate> 
            <EditItemTemplate> 
             <asp:FileUpload ID="FileUpload2" runat="server" /> 
            </EditItemTemplate> 
           </asp:TemplateField> 
           <asp:TemplateField HeaderText="File Name"> 
            <ItemTemplate> 
             <asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("fpath")%>' /> 
            </ItemTemplate> 
            <EditItemTemplate> 
             <asp:FileUpload ID="FileUpload2" runat="server" /> 
            </EditItemTemplate> 
           </asp:TemplateField> 
           <asp:TemplateField HeaderText="Description"> 
            <ItemTemplate> 
             <%#Eval("desc1")%> 
            </ItemTemplate> 
            <EditItemTemplate> 
             <asp:TextBox ID="txtdesc" runat="server" Text='<%#Eval("desc1")%>'></asp:TextBox> 
            </EditItemTemplate> 
           </asp:TemplateField> 
           <asp:CommandField HeaderText="Modify" ShowEditButton="true" EditText="Edit"> 
            <ControlStyle Width="50" /> 
           </asp:CommandField> 
           <asp:TemplateField HeaderText="Delete"> 
            <ItemTemplate> 
             <asp:LinkButton ID="lnkDelete" CommandName="Delete" runat="server" OnClientClick="return confirm('Are you sure you want to delete this record?');">Delete</asp:LinkButton> 
            </ItemTemplate> 
           </asp:TemplateField> 
          </Columns> 
         </asp:GridView> 

C#

using System; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 

using System.Data.SqlClient; 
using System.Configuration; 
using System.Data; 

public partial class _Default : System.Web.UI.Page 
{ 
    SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString); 
    SqlCommand sqlcmd = new SqlCommand(); 
    SqlDataAdapter da = new SqlDataAdapter(); 
    DataTable dt = new DataTable(); 
    String fname, fpath, desc; 
    String spath; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     Label1.Text = ""; 
     if (!Page.IsPostBack) 
     { 
      LoadGrid(); 
     } 
    } 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
     if (FileUpload1.HasFile) 
     { 
      //Check File is available in Fileupload control and then upload to server path 
      fname = FileUpload1.FileName; 
      spath = @"~\Uploaded\" + FileUpload1.FileName; 
      fpath = Server.MapPath("Uploaded"); 
      fpath = fpath + @"\" + FileUpload1.FileName; 
      desc = TextBox2.Text; 
      if (System.IO.File.Exists(fpath)) 
      { 
       Label1.Text = "File Name already exists!"; 
       return; 
      } 
      else 
      { 
       FileUpload1.SaveAs(fpath); 
      } 
      //Store details in the SQL Server table 
      StoreDetails(); 
      TextBox2.Text = ""; 
      LoadGrid(); 
     } 
     else 
     { 
      Label1.Text = "Please select file!"; 
     } 
    } 
    void StoreDetails() 
    { 
     String query; 
     query = "insert into fileDet(fname,fpath,desc1) values('" + fname + "','" + spath + "','" + desc + "')"; 
     sqlcon.Open(); 
     sqlcmd = new SqlCommand(query, sqlcon); 
     sqlcmd.CommandType = CommandType.Text; 
     sqlcmd.ExecuteNonQuery(); 
     sqlcon.Close(); 
     LoadGrid(); 
    } 
    void LoadGrid() 
    { 
     sqlcon.Open(); 
     sqlcmd = new SqlCommand("select * from fileDet", sqlcon); 
     da = new SqlDataAdapter(sqlcmd); 
     dt.Clear(); 
     da.Fill(dt); 
     if (dt.Rows.Count > 0) 
     { 
      GridView1.DataSource = dt; 
      GridView1.DataBind(); 
     } 
     else 
     { 
      GridView1.DataBind(); 
     } 
     sqlcon.Close(); 
    } 
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 
    { 
     GridView1.EditIndex = e.NewEditIndex; 
     LoadGrid(); 
    } 
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 
    { 
     GridView1.EditIndex = -1; 
     LoadGrid(); 
    } 
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) 
    { 
     String ID; 
     ID = GridView1.DataKeys[e.RowIndex].Value.ToString(); 
     sqlcmd = new SqlCommand("select * from fileDet where ID='" + ID + "'", sqlcon); 
     sqlcon.Open(); 
     da = new SqlDataAdapter(sqlcmd); 
     da.Fill(dt); 

     if (dt.Rows.Count > 0) 
     { 
      if (System.IO.File.Exists(Server.MapPath(dt.Rows[0][2].ToString()))) 
      { 
       System.IO.File.Delete(Server.MapPath(dt.Rows[0][2].ToString())); 
      } 
     } 
     sqlcmd = new SqlCommand("delete from fileDet where ID='" + ID + "'", sqlcon); 
     sqlcmd.ExecuteNonQuery(); 
     sqlcon.Close(); 
     LoadGrid(); 
    } 
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 
     GridViewRow row = GridView1.Rows[e.RowIndex]; 
     string ID; 

     ID = GridView1.DataKeys[e.RowIndex].Value.ToString(); 

     FileUpload flname = (FileUpload)row.FindControl("FileUpload2"); 

     if (flname.HasFile) 
     { 

      fname = flname.FileName; 
      spath = @"~\Uploaded\" + flname.FileName; 
      fpath = Server.MapPath("Uploaded"); 
      fpath = fpath + @"\" + flname.FileName; 
      if (System.IO.File.Exists(fpath)) 
      { 
       Label1.Text = "File Name already exists!"; 
       return; 
      } 
      else 
      { 
       flname.SaveAs(fpath); 
      } 
     } 
     else 
     { 
      Label1.Text = "Please select file!"; 
     } 

     TextBox desc1 = (TextBox)row.FindControl("txtdesc"); 
     string query; 
     query = "update fileDet set fname='" + fname + "',fpath='" + spath + "',desc1='" + desc1.Text + "' where ID='" + ID + "'"; 
     sqlcon.Open(); 
     sqlcmd = new SqlCommand(query, sqlcon); 
     sqlcmd.CommandType = CommandType.Text; 
     sqlcmd.ExecuteNonQuery(); 
     sqlcon.Close(); 
     GridView1.EditIndex = -1; 
     LoadGrid(); 
    } 
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e) 
    { 
     CheckBox chk = (CheckBox)sender; 
     GridViewRow gr = (GridViewRow)chk.Parent.Parent; 
     // lblmsg.Text = GridView1.DataKeys[gr.RowIndex].Value.ToString(); 

     //lblmsg.Text = "Hello"; 
    } 
} 
相關問題