בעיה ב Web Custom Control בתוך User Control ב ASP.NET - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

בעיה ב Web Custom Control בתוך User Control ב ASP.NET


ISmile4U

Recommended Posts

שלום,

יש לי בעיה בשילוב של Web Custom Control ב User Control ב ASP.NET .

כתבתי את ה Web Custome Control הבא אשר משמש כ RequiredFieldValidator לפקד Checkbox.


using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace CustomValidators //RequiredFieldValidatorForCheckBoxLists
{
/// <summary>
/// Summary description for WebCustomControl1.
/// </summary>

public class RequiredFieldValidatorForCheckBoxLists :
System.Web.UI.WebControls.BaseValidator
{
private ListControl _listctrl;
private int _restrictionCount;
private string _restrictionType;

public int restrictionCount
{
get
{
return _restrictionCount;
}

set
{
_restrictionCount = value;
}
}

public string restrictionType
{
get
{
return _restrictionType;
}

set
{
_restrictionType = value;
}
}

public RequiredFieldValidatorForCheckBoxLists()
{
base.EnableClientScript = false;
}

protected override bool ControlPropertiesValid()
{
Control ctrl = FindControl(ControlToValidate);

if (ctrl != null)
{
_listctrl = (ListControl) ctrl;
return (_listctrl != null);
}
else
return false; // raise exception
}

protected bool checkit()
{
int count =0;
if(_listctrl.SelectedIndex != -1)
{
foreach(ListItem li in _listctrl.Items)
{
if(li.Selected == true)
{
count++;
}
}
switch (_restrictionType)
{
case "exactly":
if(count == _restrictionCount)
{
return true;
}
else
{
return false;
}
case "up to":
if(count <= _restrictionCount)
{
return true;
}
else
{
return false;
}
default: return false;
}
}
else
{
return false;
}
}

protected override bool EvaluateIsValid()
{
return checkit();
}
}
}
using System;

כאשר אני משתמש ב Custom Control הזה ב WEB FORM רגיל, הכל עובד לי כמו שצריך והולידטור הכן עובד.

הבעיה שלי היא כאשר אני מנסה להשתמש בולידטור הזה בתוך User Control הוא לא לי את הוולידציה.

אשמח לעזרה בנידון מאחר ואני די תקוע וגילגולי ברשת לא העלו דבר בחקתם.

קישור לתוכן
שתף באתרים אחרים

נראה לי שאתה עושה שימוש לא נכון ב- CustomControl ובירושה של BaseValidator

בכל אופן אם מה שאתה מנסה לעשות עובד לך ב- דף אבל לא עובד לך תחת UserControl

רוב הסיכויים שהבעיה היא ב- ID של ה- control שה- validator צריך לבדוק

כאשר שמים control תחת usercontrol ה- ID שלו מורכב מהשם המקורי שנתת לו + קידומת שם ה- usercontrol

כיוון שה- ID של אותו control שונה, ה- validator שלך לא מצליח למצוא אותו

קישור לתוכן
שתף באתרים אחרים

הסתכלתי כבר בקוד ה HTML שמוצג למשתמש בדפדן ונכון שיש לו ID מורכב כפי שאתה מציין ID המשולב עם ה ID של ה USER CONTROL

וזה ה ID שאני מורה לבצע לו ולידציה

 Dim myctrlid As String = Me.UniqueID & "_" & mycheck.UniqueID

כאשר זה בעצם נותן לי את

_ctl0_check_1_0

למינהם שמתווצרים למשתמש ומופיעים לו ב HTML

וכפי שציינתי זה לא עובד.

קישור לתוכן
שתף באתרים אחרים

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

×
  • צור חדש...