LinkBut​​ton的一个gridview里面没有发射里面、LinkBut、ton、gridview

由网友(暗里着迷)分享简介:

aspx.cs

aspx.cs

   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改变需要火。该页面刷新,但。

So, on click of the button ID ="btnBlockUnblock", this grid view selectedindex changed needs to fire. The page is refreshing though.

谢谢 孙

推荐答案

您必须使用的GridView RowCommand 事件,而不是GridView的的SelectedIndex 更改...,例如

You have to use GridView RowCommand event instead GridView SelectedIndex Change.. e.g

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

编辑:阅读code,从您的评论后,我发现你的问题

After reading code from your comment, I found your problem.

发生了什么实际上,当你点击按钮,页面加载事件火你的GridView事件前,有你的GridView的数据重新绑定,它就会失去你的解雇事件。你要照顾你的网页提交通过把如果(!的IsPostBack)在你的页面加载,你尝试在数据绑定到GridView的。

What happened actually, when you click button, Page Load event fire before your gridview event and there your gridview data again binded and it will lost your fired event. You have to take care your page Postback by putting if(!IsPostBack) in your page load where you are trying to bind your data to 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;
     }
   }
阅读全文

相关推荐

最新文章