Linkbutton inside a gridview not firing
<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_SelectedInde开发者_StackOverflowxChanged" >
<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.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();
}
}
So, on click of the button ID ="btnBlockUnblock", this grid view selectedindex changed needs to fire. The page is refreshing though.
Thanks Sun
You have to use the GridView RowCommand
event instead of the 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();
}
}
}
Edit: After reading code from your comment, I found your problem.
What happens actually, when you click the button, the Page Load event fires before your gridview event and there your gridview data again binded and it lost your fired event
. You have to examine 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;
}
}
精彩评论