<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.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需要触发。该页面虽然令人耳目一新。
由于 太阳
答案 0 :(得分:1)
您必须使用GridView RowCommand
事件而不是GridView SelectedIndex
更改...例如
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;
}
}