2011-07-18 96 views
3
<asp:GridView ID="gvBlockUnblock" runat="server" AutoGenerateColumns="False" 
         BackColor ="AliceBlue" 
         onrowdatabound="gvBlockUnblock_RowDataBound" DataKeyNames="CPID,PUBLISHED" 
        style="margin-top: 0px" 
        AllowPaging="True" onpageindexchanging="gvBlockUnblock_PageIndexChanging" 
        PageSize="10" EnableViewState= "true" 
        onselectedindexchanged="gvBlockUnblock_SelectedIndexChanged" > 

     <Columns> 

        <asp:TemplateField HeaderText="S.No."> 
        <ItemTemplate> 
        <asp:LinkButton ID="lbSNo" runat="server" 
        Text='<%# (Eval("sno")) %>' 
        PostBackUrl='<%#"~/_UILayer/ComplaintReport.aspx?PINo="+Eval("CPID") %>' > 
        </asp:LinkButton> 
        </ItemTemplate> 
        </asp:TemplateField> 

     <asp:BoundField HeaderText = "Complaint" /> 

     <asp:HyperLinkField DataNavigateUrlFields="CPID" datatextfield = "CPID" 
       DataNavigateUrlFormatString="WebForm1.aspx?CPID={0}" HeaderText=" Problem Item No"/> 


      <asp:BoundField DataField="NewComplaints" 
       HeaderText="Number of New Complaints" SortExpression="NewComplaints" /> 
      <asp:BoundField DataField="TotalNumberofComplaints" 
       HeaderText="Total Number of Complaints" SortExpression="TotalNumberofComplaints" /> 
      <asp:BoundField DataField="NumberofUnblocks" HeaderText="Number of Unblocks" 
       SortExpression="TotalNumberofComplaints" /> 



      <asp:TemplateField HeaderText = "Comments"> 
      <ItemTemplate> 
         <asp:TextBox ID="txtAdminComment" Font-Names="Arial" ReadOnly="false" Width="200" Height="30" 
          TextMode="multiLine" runat="server" BorderStyle="NotSet"></asp:TextBox> 
        </ItemTemplate> 
       </asp:TemplateField> 


      <asp:TemplateField HeaderText = " Block/Unblock"> 
       <ItemTemplate> 
        <asp:button ID ="btnBlockUnblock" runat = "server" 
         Text = '<%# CheckBlock(Eval("PUBLISHED")) %>' CommandName="Select" 
         CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" CausesValidation="False" /> 
       </ItemTemplate> 
      </asp:TemplateField> 



        <asp:HyperLinkField DataNavigateUrlFields="CPID" Text="View Details" 
       DataNavigateUrlFormatString="ItemHistoryForm.aspx?CPID={0}" HeaderText=" Problem Item No"/> 

     </Columns> 
    </asp:GridView> 

aspx.csLinkBut​​ton的內一個gridview不點火

protected void gvBlockUnblock_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     string ComplaintProfileId = gvBlockUnblock.DataKeys[gvBlockUnblock.SelectedIndex].Values["CPID"].ToString(); 
     string ISPUBLISHED = gvBlockUnblock.DataKeys[gvBlockUnblock.SelectedIndex].Values["PUBLISHED"].ToString(); 

     string date = System.DateTime.Now.ToString(); 
     TextBox tb = (TextBox)gvBlockUnblock.Rows[gvBlockUnblock.SelectedIndex].FindControl("txtAdminComment"); 
     string Comment = tb.Text; 
     if (string.IsNullOrEmpty(Comment)) 
     { 

      WebMsgBox.Show("empty"); 
     } 
     else 
     { 
      if (ISPUBLISHED == "N") 
      { 
       ISPUBLISHED = "N"; 
      } 
      else 
      { 
       ISPUBLISHED = "Y"; 
      } 
      string AdminComment = (System.DateTime.Now.ToString() + " : " + Comment); 

      AddCommentBLL.InsertComment(AdminComment, ComplaintProfileId, ISPUBLISHED); 
      gvBlockUnblock.DataSource = AddCommentBLL.GetItem(); 
      gvBlockUnblock.DataBind(); 
     } 
    } 

所以,按鈕ID = 「btnBlockUnblock」 的點擊,這一網格視圖的selectedIndex改變需要火。該頁面雖然令人耳目一新。

感謝 孫

回答

1

你必須改用GridView控件SelectedIndex變化的GridView控件RowCommand事件..如

protected void gvBlockUnblock_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "Select") 
    { 

string ComplaintProfileId = gvBlockUnblock.DataKeys[gvBlockUnblock.SelectedIndex].Values["CPID"].ToString(); 
    string ISPUBLISHED = gvBlockUnblock.DataKeys[gvBlockUnblock.SelectedIndex].Values["PUBLISHED"].ToString(); 

    string date = System.DateTime.Now.ToString(); 
    TextBox tb = (TextBox)gvBlockUnblock.Rows[gvBlockUnblock.SelectedIndex].FindControl("txtAdminComment"); 
    string Comment = tb.Text; 
    if (string.IsNullOrEmpty(Comment)) 
    { 

     WebMsgBox.Show("empty"); 
    } 
    else 
    { 
     if (ISPUBLISHED == "N") 
     { 
      ISPUBLISHED = "N"; 
     } 
     else 
     { 
      ISPUBLISHED = "Y"; 
     } 
     string AdminComment = (System.DateTime.Now.ToString() + " : " + Comment); 

     AddCommentBLL.InsertComment(AdminComment, ComplaintProfileId, ISPUBLISHED); 
     gvBlockUnblock.DataSource = AddCommentBLL.GetItem(); 
     gvBlockUnblock.DataBind(); 
    } 
    } 
} 

編輯:從您的評論讀碼後,我發現你的問題。

實際上發生了什麼,當你點擊按鈕時,頁面加載事件在gridview事件之前觸發,並且在那裏你的gridview數據再次被綁定,並且它丟失了fired event。你必須檢查你的頁面通過將if(!IsPostBack)放入你的頁面加載中,試圖將你的數據綁定到gridview。

protected void Page_Load(object sender, EventArgs e) 
    { 
    if(!IsPostBack) 
    { 
     // gets the items table using stored proc GetItem 
     gvBlockUnblock.DataSource = AddCommentBLL.GetItem(); 
     gvBlockUnblock.DataBind(); 
     // used for paging 
     Session["MyDataSett"] = gvBlockUnblock.DataSource; 
    } 
    } 
+0

保護無效gvBlockUnblock_OnRowCommand(對象發件人,GridViewCommandEventArgs E) 我試圖代碼在OnRowCommand, 如果(e.CommandName == 「選擇」)。仍然沒有用 – Sun

+0

你可以發佈你的頁面加載事件代碼嗎? –

+0

:我在編輯 – Sun

相關問題