我有问题要禁用/启用验证器以在更新面板中向 gridview 新添加行,如果我删除更新面板,那么它工作完美,如果我添加更新面板,那么它停止工作。我已经创建了示例页面。
默认.aspx。
<head> <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $('table > tbody > tr .wrapper [class*="onoff"] option:selected').each(function (index) { adjustValidators(this); }); $('[class*="onoff"] select').change(function () { adjustValidators($('option:selected', this)[0]); }); }); </script></head>
<ajax:ToolkitScriptManager runat="server" ID="RadScriptManager1" AsyncPostBackTimeout="18000"> </ajax:ToolkitScriptManager><asp:MultiView ID="MultiView1" runat="server"> <asp:View ID="group_Group1" runat="server"> <div class="group group1"> <asp:Button ID="btnNext0_top" runat="server" Text="Next >>" OnClick="btnNext_Click" ValidationGroup="surveyValidate0" /> <asp:UpdatePanel runat="server" ID="UPNew" UpdateMode="Conditional"> <ContentTemplate> <asp:GridView runat="server" ID="repeatregion_RRSectionSS" RegionName="RRSectionSS" AutoGenerateColumns="False" GridLines="None" CellSpacing="-1" CssClass="rrsectionss"> <Columns> <asp:TemplateField> <ItemTemplate> <div id="wrapper_ONOFF" runat="server" class="wrapper onoff"> <div class='statement onoff' id="statement_ONOFF" runat="server"> Required Validator :</div> <div class='question onoff' id="questionlayer_ONOFF" runat="server"> <asp:DropDownList runat="server" ID="question_ONOFF" ValidationGroup="surveyValidate0" Data='<%# Eval("ONOFF") %>' Val='<%# Eval("ONOFF_selectedValue") %>'> <asp:ListItem Text="Off" Value="-1"></asp:ListItem> <asp:ListItem Value="1" Text="On"></asp:ListItem> </asp:DropDownList> </div> </div> <div id="wrapper_testsssss" runat="server" class="wrapper testsssss"> <div class='statement testsssss' id="statement_testsssss" runat="server"> Sample Text </div> <div class='question testsssss' id="questionlayer_testsssss" runat="server"> <asp:TextBox runat="server" ID="question_testsssss" type="shortans" Columns='40' ValidationGroup="surveyValidate0" Text='<%# Common.DecodeXML(Eval("testsssss")) %>'></asp:TextBox> <asp:RequiredFieldValidator ID="rfv_question_testsssss" runat="server" ValidationGroup="surveyValidate0" SetFocusOnError="true" EnableClientScript="true" ControlToValidate="question_testsssss" Display="Dynamic" Text="**" ErrorMessage="**" /> </div> </div> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:Button runat="server" ID="btnAddClick" Text="Add Section" OnClick="btn_Click" /> </ContentTemplate> </asp:UpdatePanel> </div> </asp:View> <asp:View ID="group_Group2" runat="server"> <div class="group group2"> <asp:Button ID="btnPrevious1_top" runat="server" Text="<< Prev" OnClick="btnPrevious_Click" /> <asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="surveyValidate1" /> </div> </asp:View> </asp:MultiView>
<script type="text/javascript"> function pageLoad(sender, args) { if (args.get_isPartialLoad()) { $('table > tbody > tr .wrapper [class*="onoff"] option:selected').each(function (index) { adjustValidators(this); }); } } function adjustValidators(ctrl) { var id = ctrl.parentNode.id.replace('_question', '_rfv_question').replace('ONOFF', 'testsssss') // alert(ctrl.value); if (ctrl.value == -1) { ValidatorEnable(eval(id), false) } else { ValidatorEnable(eval(id), true) } } </script>
默认.aspx.cs。
protected void Page_Load(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = (MultiView1.ActiveViewIndex == -1) ? 0 : MultiView1.ActiveViewIndex;
DataTable objData =new DataTable();
if (ViewState["data"] != null)
{
objData = (DataTable)ViewState["data"];
}
else
{
objData = new DataTable();
objData.Columns.Add("testsssss");
objData.Columns.Add("ONOFF");
objData.Columns.Add("ONOFF_selectedValue");
objData.Rows.Add(objData.NewRow());
ViewState["data"] = objData;
}
if (!IsPostBack)
{
repeatregion_RRSectionSS.DataSource = objData;
repeatregion_RRSectionSS.DataBind();
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex += 1;
}
protected void btnPrevious_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex -= 1;
}
protected void btn_Click(object sender, EventArgs e)
{
DataTable objData = (DataTable)ViewState["data"];
objData.Rows.Add(objData.NewRow());
repeatregion_RRSectionSS.DataSource = objData;
repeatregion_RRSectionSS.DataBind();
ViewState["data"] = objData;
}
当点击添加然后项目被添加到gridview但验证器没有被禁用。
感谢和问候萨迪克莫丹