我在gridview中有一个复选框。在CheckedChanged事件上,它将更新发送到数据库。它工作正常,直到我添加一条消息询问用户他/她是否想继续。
单击该复选框时,将显示该消息,用户单击确定并且不会触发checkchanged事件。但是,刷新页面时,会触发checkchanged事件。
我尝试了很多组合,但似乎没有任何效果。
这是带有2个复选框的gridview:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellSpacing="0" GridLines="None" PageSize="20"
ShowStatusBar="false" ShowFooter="False" ShowHeader="false" DataKeyNames="RowType, QID"
AllowFilteringByColumn="false" AllowPaging="false" AllowSorting="false" ShowGroupPanel="false" Skin="" Width="100%"
CssClass="GVResult" SelectedItemStyle-CssClass="SelectedStyle" RenderMode="Lightweight"
OnRowDataBound="GV_ItemDataBound" OnRowCommand="GV_RowCommand">
<Columns>
<asp:TemplateField Visible="true" ItemStyle-HorizontalAlign="Left" HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblQuestionNo" Text='<%# Eval("QuestionNo") %>' runat="Server" CssClass='<%# Eval("CSS") %>' Enabled="False" />
<asp:HiddenField ID="hfQID" runat="Server" Value='<%# Eval("QID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="true" ItemStyle-HorizontalAlign="Left" HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblMarker1" Text='<%# Eval("Marker1") %>' runat="Server" CssClass='<%# Eval("CSS") %>' Enabled="true" />
<asp:CheckBox ID="ckMarker1" Checked='<%# Eval("CheckMarker1") %>' runat="Server" AutoPostBack="true" OnCheckedChanged="ckMarker_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="true" ItemStyle-HorizontalAlign="Left" HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblMarker2" Text='<%# Eval("Marker2") %>' runat="Server" CssClass="Label_100_White" Enabled="False" />
<asp:CheckBox ID="ckMarker2" Checked='<%# Eval("CheckMarker2") %>' runat="Server" Enabled="true" AutoPostBack="true" OnCheckedChanged="ckMarker_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
复选框的消息添加在gridview RowDataBound上:
protected void GV_ItemDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row is GridViewRow & e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow Item = e.Row as GridViewRow;
CheckBox ckMarker1 = (CheckBox)e.Row.FindControl("ckMarker1");
CheckBox ckMarker2 = (CheckBox)e.Row.FindControl("ckMarker2");
ckMarker1.Attributes.Add("onclick", "javascript: var answer = confirm('Do you want to proceed with the changes');return answer;");
ckMarker2.Attributes.Add("onclick", "javascript: var answer = confirm('Do you want to proceed with the changes');return answer;"); }
}
这是复选框更改事件:在显示消息后触发,而不是在刷新页面时触发。
protected void ckMarker_CheckedChanged(object sender, EventArgs e)
{
DTO.FinalMarkingUpdate f = new DTO.FinalMarkingUpdate();
BLL.AllocMQuestion F = new BLL.AllocMQuestion();
CheckBox ck = (CheckBox)sender;
GridViewRow Item = (GridViewRow)ck.NamingContainer;
f.OrgID = this.OrgID;
f.AssessID = this.AssessID;
f.MarkerNo = int.Parse(ck.ID.Substring(8, 1));
f.QID = int.Parse(((HiddenField)Item.FindControl("hfQID")).Value);
f.IsMarker = ck.Checked;
F.UpdateMarkerAndChecker(f);
DataSource();
}
添加如下所示的属性以使其工作。它将返回true
确认dialogbox
确定点击并返回false确认dialogbox
取消点击。
protected void GV_ItemDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row is GridViewRow & e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow Item = e.Row as GridViewRow;
CheckBox ckMarker1 = (CheckBox)e.Row.FindControl("ckMarker1");
CheckBox ckMarker2 = (CheckBox)e.Row.FindControl("ckMarker2");
ckMarker1.Attributes.Add("onclick", "if (!confirm('Do you want to proceed with the changes')) return false;");
ckMarker2.Attributes.Add("onclick", "if (!confirm('Do you want to proceed with the changes')) return false;");
}
}