ASP.NET按钮不运行相关功能

时间:2012-04-25 23:12:10

标签: asp.net

我有UpdatePanel,其中GridView,其中Repeater。有一个按钮可以添加评论。此按钮后退,但id不运行后面的功能代码。这是我的代码

<updatepanel>
    <gridview>
        <repeater>
            <asp:Button ID="kitapVarYorumEkle" runat="server"  
                 CommandArgument='<%#Eval("id") %>' 
                 CommandName='<%# GridView1.Rows.Count.ToString() %>' 
                 Text="Yorum ekle" class="blueButton" 
                 OnClick="kitapVarYorumEkle_Click"  />
        </repeater>
    </gridview>
</updatepanel>

js中的代码是:

protected void kitapVarYorumEkle_Click(object sender, EventArgs e)
{
        kitapVarYorum temp = new kitapVarYorum();

        if (Session["id"] != null)
        {
            temp.uyeId = Convert.ToInt32(Session["id"]);
        }

        Button btn = (Button)sender;
        temp.kitapVarId = Convert.ToInt32(btn.CommandArgument);

        string text = "";

        GridViewRow row = GridView1.Rows[Convert.ToInt32(btn.CommandName)];

        if (row != null)
        {
            TextBox txt = row.FindControl("txtYorum") as TextBox;

            if (txt != null)
            {
                text = txt.Text;
            }
        }

        temp.icerik = text;
        var db = Tools.DBBaglanti();
        db.kitapVarYorums.InsertOnSubmit(temp);
        db.SubmitChanges();

        // Page.Response.Redirect(Page.Request.Url.ToString(), true);
    }
}

我在控件之外尝试了它并且它可以正常工作

1 个答案:

答案 0 :(得分:0)

您是否尝试过ItemCommand方法?

<asp:Button  
     CommandArgument='<%#Eval("id") %>'
     CommandName="yourCommandNameOfChoice" 
     Text="Yorum ekle" 
     ID="kitapVarYorumEkle" 
     class="blueButton" runat="server"  
     OnClick="kitapVarYorumEkle_Click"  />

现在为您的转发器添加ItemCommand事件:

 protected void yourRepeater_ItemCommand(object source , RepeaterCommandEventArgs e)
 {
     switch(e.CommandName)
     {
           case "yourCommandNameOfChoice":
               // Your Code Here 
               break;
     }
 }