Connect Local AD with C# .Net Webform - c#

Having trouble understanding this error below in vs.net.
I'm trying to grab a logged in user account on the domain and allow them to be able to edit their phone number.
Another guy setup the AD Access, but I can't event get a logged in user name.
Pasted the code VS.NET errors on every time with the exception that I found in an online article and worked for everyone else except me.
I've verified it works using powershell but I could use some help.
THANKS!!!
//parse the current user's logon name as search key
string sFilter = String.Format(
"(&(objectClass=user)(objectCategory=person)(sAMAccountName={0}))",
User.Identity.Name.Split(new char[] { '\\' })[1]
);
Exception User-Unhandled
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
<%# Import Namespace="System.DirectoryServices.ActiveDirectory" %>
<%# Import Namespace="System.DirectoryServices.AccountManagement" %>
<!DOCTYPE html>
<head>
<script language="c#" runat="server">
static string adsPath = "LDAP://dc=DOMAIN,dc=com";
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
SearchResult sr = FindCurrentUser(new string[] { "allowedAttributesEffective" });
if (sr == null)
{
msg.Text = "User not found...";
return;
}
int count = sr.Properties["allowedAttributesEffective"].Count;
if (count > 0)
{
int i = 0;
string[] effectiveAttributes = new string[count];
foreach (string attrib in sr.Properties["allowedAttributesEffective"])
{
effectiveAttributes[i++] = attrib;
}
sr = FindCurrentUser(effectiveAttributes);
foreach (string key in effectiveAttributes)
{
string val = String.Empty;
if (sr.Properties.Contains(key))
{
val = sr.Properties[key][0].ToString();
}
GenerateControls(key, val, parent);
}
}
}
else
{
UpdateControls();
}
}
private SearchResult FindCurrentUser(string[] attribsToLoad)
{
//parse the current user's logon name as search key
string sFilter = String.Format(
"(&(objectClass=user)(objectCategory=person)(sAMAccountName={0}))",
User.Identity.Name.Split(new char[] { '\\' })[1]
);
DirectoryEntry searchRoot = new DirectoryEntry(
adsPath,
null,
null,
AuthenticationTypes.Secure
);
using (searchRoot)
{
DirectorySearcher ds = new DirectorySearcher(
searchRoot,
sFilter,
attribsToLoad,
SearchScope.Subtree
);
ds.SizeLimit = 1;
return ds.FindOne();
}
}
private void GenerateControls(string attrib, string val, Control parent)
{
parent.Controls.Add(new LiteralControl("<div>"));
TextBox t = new TextBox();
t.ID = "c_" + attrib;
t.Text = val;
t.CssClass = "txt";
Label l = new Label();
l.Text = attrib;
l.AssociatedControlID = t.ID;
l.CssClass = "lbl";
parent.Controls.Add(l);
parent.Controls.Add(t);
parent.Controls.Add(new LiteralControl("</div>"));
}
private void UpdateControls()
{
SearchResult sr = FindCurrentUser(new string[] { "cn" });
if (sr != null)
{
using (DirectoryEntry user = sr.GetDirectoryEntry())
{
foreach (string key in Request.Form.AllKeys)
{
if (key.StartsWith("c_"))
{
string attrib = key.Split(new char[] { '_' })[1];
string val = Request.Form[key];
if (!String.IsNullOrEmpty(val))
{
Response.Output.Write("Updating {0} to {1}<br>", attrib, val);
user.Properties[attrib].Value = val;
}
}
}
user.CommitChanges();
}
}
btnSubmit.Visible = false;
Response.Output.Write("<br><br>< Back", Request.Url);
}
</script>
<style>
.lbl
{
margin-left: 25px;
clear: left;
width: 250px;
}
.txt
{
width: 250px;
}
</style>
</head>
<body>
<form id="main" runat="server">
Data for user:
<%=User.Identity.Name%>
<br>
<br>
<asp:Label ID="msg" runat="server" />
<asp:Panel ID="parent" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Update" />
</form>
</body>
</html>

Related

Check for empty data in c#

I am using c# and I have a Hidden Field
<asp:HiddenField runat="server" ID="hidJsonHolder" ClientIDMode="Static" />
How do I add a alert, so that I can check for empty data object obj get from Hidden Field ?
I have tried with RegularExpressionValidator but reply error
<asp:HiddenField runat="server" ID="hidJsonHolder" ClientIDMode="Static" />
<asp:RegularExpressionValidator Display="Dynamic"
ControlToValidate="hidJsonHolder"
ID="RegularExpressionValidator1"
runat="server" ErrorMessage="error"
ValidationGroup="Validation2"></asp:RegularExpressionValidator>
This other code not alert
protected void btnFinal_Click(object sender, EventArgs e)
{
JavaScriptSerializer jsSer = new JavaScriptSerializer();
object obj = jsSer.DeserializeObject(hidJsonHolder.Value);
if (obj != null)
{
Movie[] listMovie = jsSer.ConvertToType<Movie[]>(obj);
foreach (Movie p in listMovie)
{
string pattern = #"\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*";
Regex re = new Regex(pattern);
if (p.ToString() != null)
{
MatchCollection matches = re.Matches(p.ToString());
if (matches.Count > 0)
{
for (int i = 0; i < matches.Count; i++)
{
Response.Write(matches[i] + "; ");
}
}
}
}
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('Error.');", true);
}
}
Edit #1
Like this:
protected void btnFinal_Click(object sender, EventArgs e)
{
string val = hidJsonHolder.Value.Replace("[]","");
if (!String.IsNullOrEmpty(val.ToString()))
{
JavaScriptSerializer jsSer = new JavaScriptSerializer();
object obj = jsSer.DeserializeObject(hidJsonHolder.Value);
if (obj != null)
{
Movie[] listMovie = jsSer.ConvertToType<Movie[]>(obj);
foreach (Movie p in listMovie)
{
string pattern = #"\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*";
Regex re = new Regex(pattern);
if (p.ToString() != null)
{
MatchCollection matches = re.Matches(p.ToString());
if (matches.Count > 0)
{
for (int i = 0; i < matches.Count; i++)
{
Response.Write("<br />" + matches[i] + "; ");
}
}
}
}
}
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('Error.');", true);
}
}

implementing Invisible Google reCaptcha for asp.net application ?

This is my ASP.NET form. I want to add invisible recaptcha to it with server side validation. Can someone please help?
I can do client side validation but it doesnt use secret key. My another questions is Do we need secret key for invisible recaptcha?
Please see serverside code that i used for google recaptcha but it is not working for Invisible recaptcha. I am getting this error : -
reCAPTCHA Error: missing-input-response: Not Valid Recaptcha
<div id="ContactFormDiv" runat="server">
<div class="form-row form-required">
<asp:Label ID="YourNameLabel" runat="server" AssociatedControlID="YourNameTextBox"> Your Name:</asp:Label>
<asp:TextBox ID="YourNameTextBox" runat="server" CssClass="form300" MaxLength="150"></asp:TextBox>
</div>
<div class="form-row form-required">
<div id='recaptcha' class="g-recaptcha"
data-sitekey="site key"
data-callback="onSubmit"
data-size="invisible">
</div>
</div>
<div class="form-row-buttons">
<asp:Button ID="SendMessageButton" ClientIDMode="Static" runat="server" Text="Send Message" CssClass="buttonPositive"
CausesValidation="True" OnClick="SendMessageButton_Click" />
</div>
</div>
Javascript Code
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js" async defer></script>
Serverside Code
public class MyObject
{
public string success { get; set; }
}
public static string ReCaptcha_Key = "------------------Site Key-----------------";
public static string ReCaptcha_Secret = "--------------Secret Key ---------------";
public bool ValidateReCaptcha()
{
bool Valid = false;
//start building recaptch api call
var sb = new StringBuilder();
//Getting Response String Append to Post Method
string Response = Request["g-recaptcha-response"];
string url = "https://www.google.com/recaptcha/api/siteverify?secret=" + ReCaptcha_Secret + "&response=" + Response;
sb.Append(url);
//make the api call and determine validity
using (var client = new WebClient())
{
var uri = sb.ToString();
var json = client.DownloadString(uri);
var serializer = new DataContractJsonSerializer(typeof(RecaptchaApiResponse));
var ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
var result = serializer.ReadObject(ms) as RecaptchaApiResponse;
//--- Check if we are able to call api or not.
if (result == null)
{
lblmsg.Text = "Captcha was unable to make the api call";
}
else // If Yes
{
//api call contains errors
if (result.ErrorCodes != null)
{
if (result.ErrorCodes.Count > 0)
{
foreach (var error in result.ErrorCodes)
{
lblmsg.Text = "reCAPTCHA Error: " + error;
}
}
}
else //api does not contain errors
{
if (!result.Success) //captcha was unsuccessful for some reason
{
lblmsg.Text = "Captcha did not pass, please try again.";
}
else //---- If successfully verified. Do your rest of logic.
{
lblmsg.Text = "Captcha cleared ";
Valid = true;
}
}
}
}
return Valid;
}
public bool temp = true;
protected void SendMessageButton_Click(object sender, EventArgs e)
{
temp = ValidateReCaptcha();
if (temp == false)
{
lblmsg.Text = "Not Valid Recaptcha";
lblmsg.ForeColor = System.Drawing.Color.Red;
}
else
{
lblmsg.Text = "Successful";
lblmsg.ForeColor = System.Drawing.Color.Green;
}
Page.Validate();
if (this.Page.IsValid == true && temp == true)
{ //Page and invisible recaptcha is valid }
}
I am getting this error : -
reCAPTCHA Error: missing-input-response: Not Valid Recaptcha
This is how I implemented the working sample:
-- Client Side (Refer to Google Documentation )
<head>
<!-- Google Invisible Captcha -->
<script src='https://www.google.com/recaptcha/api.js'/>
<script>
function onSubmit(token) {
document.getElementById("htmlForm").submit();
}
</script>
</head>
<body>
<form id="htmlForm" action="Default.aspx" method="post">
<input name="txtName" />
<input name="txtEmailAddress" />
<button class="g-recaptcha btn btn-default"
data-sitekey="-------------------Site key--------------"
data-callback="onSubmit">
Submit Request
</button>
</form>
</body>
-- Server Side (keeps secret Key)
public static bool IsValidCaptcha()
{
var secret = "--------------Secret Key ---------------";
var req =
(HttpWebRequest)
WebRequest.Create("https://www.google.com/recaptcha/api/siteverify?secret=" + secret + "&response=" + HttpContext.Current.Request.Form["g-recaptcha-response"]);
using (var wResponse = req.GetResponse())
{
using (StreamReader readStream = new StreamReader(wResponse.GetResponseStream()))
{
string responseFromServer = readStream.ReadToEnd();
if (!responseFromServer.Contains("\"success\": false"))
return true;
}
}
return false;
}
I also have similar problem and it looks like it is harder to find any decent example. However, I saw that you have set
data-callback="onSubmit"
but I didn't see where you have defined that method. Is it there? Could that be what are you missing?

Add code in page base to header C#

I have the following code in my MasterPageBase.cs file:
protected override void OnLoad(EventArgs e)
{
string url = Request.Path;
var page = _ContentPageRepository.GetContentPageByUrl(url, ConfigurationManager.GetSiteID());
if (page != null)
{
PageBase.SetTitle(page.Title);
PageBase.SetDescription(page.Description);
PageBase.SetKeywords(page.Keywords);
}
else
{
this.ProcessSiteMap();
}
this.AddGACode();
base.OnLoad(e);
}
I need this.AddGACode(); to get added to the head section of the page, but when I view the source of the page as I am running the solution, I see that this is adding it to the body section of the page.
I have tried Page.Header.Controls.Add(AddGACode()); and get the following errors:
The best overloaded method match has some invalid arguments and cannot convert from 'void' to 'System.Web.UI.Control'
What can I do to get this code added to the head? TIA
EDIT: Request to see the AddGACode method:
private void AddGACode()
{
var gaCode = SiteManager.GetSite().GoogleAnalyticsCode;
if (!String.IsNullOrEmpty(gaCode) && Response.StatusCode == 200)
{
if (!ConfigurationManager.EnableUniversalGATracking)
{
ClientScriptManager cs = Page.ClientScript;
StringBuilder csText = new StringBuilder();
csText.Append("<script type=\"text/javascript\">");
csText.Append(String.Format("var _gaq = _gaq || []; _gaq.push(['_setAccount', '{0}']); ", gaCode));
csText.Append("_gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();");
csText.Append("</script>");
cs.RegisterClientScriptBlock(GetType(), "GACode", csText.ToString());
}
else
{
ClientScriptManager cs = Page.ClientScript;
StringBuilder csText = new StringBuilder();
csText.Append("<!-- Universal GA Code --><script type=\"text/javascript\">");
csText.Append(String.Concat("(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', '", gaCode, " ', 'auto'); ga('send', 'pageview');"));
csText.Append("</script>");
cs.RegisterClientScriptBlock(GetType(), "GACode", csText.ToString());
}
}
}
EDIT:
This code is in the AddGACode method. There is still this.AddGACode(); in the OnLoad of the page that seems to duplicate the code with this edit, but both codes will disappear if I delete this.AddGACode(); from OnLoad
ClientScriptManager cs = Page.ClientScript;
StringBuilder csText = new StringBuilder();
csText.Append("<!-- Universal GA Code --><script type=\"text/javascript\">");
csText.Append(String.Concat("(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', '", gaCode, " ', 'auto'); ga('send', 'pageview');"));
csText.Append("</script>");
cs.RegisterClientScriptBlock(GetType(), "GACode", csText.ToString());
LiteralControl lc = new LiteralControl(csText.ToString());
Page.Header.Controls.Add(lc);
This adds the script into the head tag:
LiteralControl lt = new LiteralControl("<script type='text/javascript'>alert('test');</script>");
Header.Controls.Add(lt);
UPDATE
LiteralControl lt = new LiteralControl(AddGACode());
Header.Controls.Add(lt);
...
private string AddGACode()
{
var result = string.Empty;
var gaCode = SiteManager.GetSite().GoogleAnalyticsCode;
if (!String.IsNullOrEmpty(gaCode) && Response.StatusCode == 200)
{
StringBuilder csText = new StringBuilder();
csText.Append("<script type=\"text/javascript\">");
if (!ConfigurationManager.EnableUniversalGATracking)
{
csText.Append(String.Format("var _gaq = _gaq || []; _gaq.push(['_setAccount', '{0}']); ", gaCode));
csText.Append("_gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();");
}
else
{
csText.Append(String.Concat("(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', '", gaCode, " ', 'auto'); ga('send', 'pageview');"));
}
csText.Append("</script>");
result = csText.ToString();
}
return result;
}
I'd keep your markup, in this instance your ga scripts, on mastpage itself. In the head tag add two literals, gaGaq and gaUniversal an then use you logic to contol the visibility of them.
<head runat="server"><script type="text\javascript">
<asp:Literal id="gaGaq" runat="server">
<!-- Put you gaq code here-->
<!-- Keep {0} as a place holder for gaqCode -->
</script>
</asp:Literal>
<asp:Literal id="gaUniveral" runat="server">
<script type="text\javascrtip">
<!-- Put you universal code here-->
<!-- Keep {0} as a place holder for gaqCode -->
</script>
</asp:Literal>
</head>
C#
private void AddGACode()
{
var gaCode = SiteManager.GetSite().GoogleAnalyticsCode;
if (!String.IsNullOrEmpty(gaCode) && Response.StatusCode == 200)
{
if(ConfigurationManager.EnableUniversalGATracking)
{
//Set gaCode
gaUniversal.Text = string.Fomat(gaUniveral.Text, gaCode);
}
else
{
//Set gaCode
gaGaq.Text = string.Format(ga.Text, gaCode);
}
gaUniversal.Visible = ConfigurationManager.EnableUniversalGATracking;
gaGaq.Visible = !ConfigurationManager.EnableUniversalGATracking;
}
else
{
//Hide Both literals if no gaCode
gaUniversal.Visible = gaGaq.Visible = false;
}
}
You could also look at putting all this into a custom control. If you're interested in taking that route I wrote a blog article on exactly that for the gaq style google analytics so I could drop it onto my many asp.net websites. The code in that article owuld need to be modified to suite your needs but should be enough to get you stared.

Custom BoundField doesn't preserve data between PostBacks with added LiteralControl

I've created a custom BoundField for use with a DetailsView in Edit and Insert modes to utilize Twitter Bootstrap.
The control works fine, except when I add in extra LiteralControls to surround the TextBox with some HTML during InitializeDataCell(...). Code related to this toward the end.
The HTML generated is below seems to be identical to what is generated with a TemplateField. But when I fire an Update, anytime it adds the extra div / span seen here, the value in the TextBox is blank upon PostBack.
<div class="input-prepend">
<span class="add-on">$</span>
<input name="ctl00$ctl00$MainContent$MainContent$pi$dvPackage$tbPrice" type="text" value="0" size="5" id="MainContent_MainContent_pi_dvPackage_tbPrice" title="Price">
</div>
Here is some examples of what I'm doing within the front ASP.NET code and what HTML is generated.
Doesn't work - custom field
Value inside text box is unset after submitting via Update command, ONLY when I add the input-append and add-on part.
Field inside DetailsView
<my:ValidationField DataField="Price" HeaderText="Price" TextPrepend="$" />
HTML generated
<td>
<div class="input-prepend">
<span class="add-on">$</span>
<input name="ctl00$ctl00$MainContent$MainContent$pi$dvPackage$tbPrice" type="text" value="0" size="5" id="MainContent_MainContent_pi_dvPackage_tbPrice" title="Price">
</div>
</td>
Does work - normal TemplateField
Works fine
Field inside DetailsView
<asp:TemplateField>
<EditItemTemplate>
<div class="input-prepend">
<span class="add-on">$</span>
<asp:TextBox ID="tbCost" runat="server" Text='<%# Bind("Cost") %>'></asp:TextBox>
</div>
</EditItemTemplate>
</asp:TemplateField>
HTML generated - identical to what is above
<td>
<div class="input-prepend">
<span class="add-on">$</span>
<input name="ctl00$ctl00$MainContent$MainContent$pi$dvPackage$tbPrice" type="text" value="0" size="5" id="MainContent_MainContent_pi_dvPackage_tbPrice" title="Price">
</div>
</td>
Does work - HTML generated with custom field
Value inside text box is correctly submitted via Update command when I don't add the TextPrepend field.
Field inside DetailsView
<my:ValidationField DataField="Price" HeaderText="Price" />
HTML generated without extra span / div
<td>
<input name="ctl00$ctl00$MainContent$MainContent$pi$dvPackage$tbPrice" type="text" value="0" size="5" id="MainContent_MainContent_pi_dvPackage_tbPrice" title="Price">
</td>
InitializeDataCell parts related to this code creation
I believe this is due to something with the InitializeDataCell(...) implementation.
protected override void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState)
{
base.InitializeDataCell(cell, rowState);
// Find the text box to validate
TextBox text = FindTextBox(cell);
if (text != null)
{
text.ID = "tb" + DataField;
text.MaxLength = MaxLength;
text.TextMode = TextMode;
text.Text = DataField;
string cellCss = string.Empty;
bool prepend = !string.IsNullOrEmpty(this.TextPrepend);
bool append = !string.IsNullOrEmpty(this.TextAppend);
bool addon = prepend || append;
if (prepend == true)
cellCss = this.ConcatenateCss(cellCss, "input-prepend");
if (append == true)
cellCss = this.ConcatenateCss(cellCss, "input-append");
if (addon == true)
{
int textIndex = cell.Controls.IndexOf(text);
Literal container = new Literal();
container.Text = "<div class=\"" + cellCss + "\">";
cell.Controls.AddAt(textIndex, container);
}
if (prepend == true)
{
int textIndex = cell.Controls.IndexOf(text);
Literal units = new Literal();
units.Text = "<span class=\"add-on\">" + this.Prepend() + "</span>";
cell.Controls.AddAt(textIndex, units);
}
if (append == true)
{
Literal units = new Literal();
units.Text = "<span class=\"add-on\">" + this.Append() + "</span>";
cell.Controls.Add(units);
}
if (addon == true)
{
Literal container = new Literal();
container.Text = "</div>";
cell.Controls.Add(container);
}
}
}
Entire code in case it's useful to anyone trying to use Twitter Bootstrap, or if my code is wrong elsewhere.
public class ValidationField : BoundField
{
#region Properties
public virtual string EditTextCssClass
{
get
{
return (string)(ViewState["EditTextCssClass"] ?? string.Empty);
}
set
{
ViewState["EditTextCssClass"] = value;
OnFieldChanged();
}
}
public virtual ValidatorDisplay ErrorDisplay
{
get
{
return (ValidatorDisplay)(ViewState["ErrorDisplay"] ?? ValidatorDisplay.Dynamic);
}
set
{
ViewState["ErrorDisplay"] = value;
OnFieldChanged();
}
}
public virtual string ErrorMessage
{
get
{
return (string)(ViewState["ErrorMessage"] ?? "Invalid value entered");
}
set
{
ViewState["ErrorMessage"] = value;
OnFieldChanged();
}
}
public virtual string HelpText
{
get
{
return (string)(ViewState["HelpText"] ?? string.Empty);
}
set
{
ViewState["HelpText"] = value;
OnFieldChanged();
}
}
public virtual TwitterBootstrap.WebControls.TextBox.HelpTextDisplayMode HelpDisplay
{
get
{
return (TwitterBootstrap.WebControls.TextBox.HelpTextDisplayMode)(ViewState["HelpDisplay"] ?? TwitterBootstrap.WebControls.TextBox.HelpTextDisplayMode.Block);
}
set
{
ViewState["HelpDisplay"] = value;
OnFieldChanged();
}
}
public virtual int MaxLength
{
get
{
return (int)(ViewState["MaxLength"] ?? 0);
}
set
{
ViewState["MaxLength"] = value;
OnFieldChanged();
}
}
public virtual string PlaceHolder
{
get
{
return (string)(ViewState["PlaceHolder"] ?? string.Empty);
}
set
{
ViewState["PlaceHolder"] = value;
OnFieldChanged();
}
}
public virtual bool Required
{
get
{
return (bool)(ViewState["Required"] ?? false);
}
set
{
ViewState["Required"] = value;
OnFieldChanged();
}
}
public virtual TextBoxMode TextMode
{
get
{
return (TextBoxMode)(ViewState["TextMode"] ?? TextBoxMode.SingleLine);
}
set
{
ViewState["TextMode"] = value;
OnFieldChanged();
}
}
public virtual string ValidationExpression
{
get
{
return (string)(ViewState["ValidationExpression"] ?? string.Empty);
}
set
{
ViewState["ValidationExpression"] = value;
OnFieldChanged();
}
}
public virtual string ValidationGroup
{
get
{
return (string)(ViewState["ValidationGroup"] ?? string.Empty);
}
set
{
ViewState["ValidationGroup"] = value;
OnFieldChanged();
}
}
public virtual string TextAppend
{
get
{
object value = ViewState["TextAppend"];
if (value != null)
return value.ToString();
return string.Empty;
}
set
{
ViewState["TextAppend"] = value;
OnFieldChanged();
}
}
public virtual string TextPrepend
{
get
{
object value = ViewState["TextPrepend"];
if (value != null)
return value.ToString();
return string.Empty;
}
set
{
ViewState["TextPrepend"] = value;
OnFieldChanged();
}
}
#endregion
public ValidationField()
{
}
protected override DataControlField CreateField()
{
return new ValidationField();
}
protected override void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState)
{
base.InitializeDataCell(cell, rowState);
// Find the text box to validate
TextBox text = FindTextBox(cell);
if (text != null)
{
text.ID = "tb" + DataField;
text.MaxLength = MaxLength;
text.TextMode = TextMode;
text.CssClass = EditTextCssClass;
text.Text = DataField;
if (PlaceHolder != string.Empty)
text.Attributes.Add("placeholder", PlaceHolder);
string cellCss = string.Empty;
bool prepend = !string.IsNullOrEmpty(this.TextPrepend);
bool append = !string.IsNullOrEmpty(this.TextAppend);
bool addon = prepend || append;
if (prepend == true)
cellCss = this.ConcatenateCss(cellCss, "input-prepend");
if (append == true)
cellCss = this.ConcatenateCss(cellCss, "input-append");
if (addon == true)
{
int textIndex = cell.Controls.IndexOf(text);
Literal container = new Literal();
container.Text = "<div class=\"" + cellCss + "\">";
cell.Controls.AddAt(textIndex, container);
}
if (prepend == true)
{
int textIndex = cell.Controls.IndexOf(text);
Literal units = new Literal();
units.Text = "<span class=\"add-on\">" + this.Prepend() + "</span>";
cell.Controls.AddAt(textIndex, units);
}
if (append == true)
{
Literal units = new Literal();
units.Text = "<span class=\"add-on\">" + this.Append() + "</span>";
cell.Controls.Add(units);
}
if (addon == true)
{
Literal container = new Literal();
container.Text = "</div>";
cell.Controls.Add(container);
}
if (Required == true)
{
Literal required = new Literal();
required.Text = "<span class=\"required\">*</span>";
cell.Controls.Add(required);
}
if (HelpText != string.Empty)
{
Label lblHelpText = new Label();
if (HelpDisplay == TwitterBootstrap.WebControls.TextBox.HelpTextDisplayMode.Block)
lblHelpText.CssClass = "help-block";
else
lblHelpText.CssClass = "help-inline";
lblHelpText.Text = HelpText;
cell.Controls.Add(lblHelpText);
}
if (Required == true)
{
// Add a RequiredFieldValidator
RequiredFieldValidator required = new RequiredFieldValidator();
required.ErrorMessage = ErrorMessage;
required.Display = ErrorDisplay;
required.ControlToValidate = text.ID;
required.Text = "";
if (ValidationGroup != string.Empty)
required.ValidationGroup = ValidationGroup;
cell.Controls.Add(required);
}
if (ValidationExpression != string.Empty)
{
// Add a RequiredFieldValidator
RegularExpressionValidator regex = new RegularExpressionValidator();
regex.ErrorMessage = ErrorMessage;
regex.Display = ErrorDisplay;
regex.ControlToValidate = text.ID;
regex.ValidationExpression = ValidationExpression;
if (ValidationGroup != string.Empty)
regex.ValidationGroup = ValidationGroup;
cell.Controls.Add(regex);
}
}
}
#region Methods
private string ConcatenateCss(params string[] classes)
{
string result = string.Empty;
foreach (string s in classes)
result += s + " ";
return result.TrimEnd().TrimStart();
}
private static TextBox FindTextBox(Control parent)
{
TextBox result = null;
foreach (Control control in parent.Controls)
{
if (control is TextBox)
{
result = control as TextBox;
break;
}
}
return result;
}
protected virtual string Prepend()
{
return this.TextPrepend;
}
protected virtual string Append()
{
return this.TextAppend;
}
#endregion
}
I managed to figure out why this was happening. Since the Control collection was altered in the field, you must also override _ExtractValuesFromCell(...)to get theTextBox` value.
I have used a slightly altered implementation from ASP.NET Boundfield overridden to support Dropdownlist is missing one final feature and haven't optimized for my use case. However, it works fine now.
public override void ExtractValuesFromCell(System.Collections.Specialized.IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
{
Control control = null;
string dataField = DataField;
object text = null;
string nullDisplayText = NullDisplayText;
if (((rowState & DataControlRowState.Insert) == DataControlRowState.Normal) || InsertVisible)
{
if (cell.Controls.Count > 0)
{
foreach (Control c in cell.Controls)
{
control = cell.Controls[0];
if (c is TextBox)
{
text = ((TextBox)c).Text;
}
}
}
else if (includeReadOnly)
{
string s = cell.Text;
if (s == " ")
{
text = string.Empty;
}
else if (SupportsHtmlEncode && HtmlEncode)
{
text = HttpUtility.HtmlDecode(s);
}
else
{
text = s;
}
}
if (text != null)
{
if (((text is string) && (((string)text).Length == 0)) && ConvertEmptyStringToNull)
{
text = null;
}
if (((text is string) && (((string)text) == nullDisplayText)) && (nullDisplayText.Length > 0))
{
text = null;
}
if (dictionary.Contains(dataField))
{
dictionary[dataField] = text;
}
else
{
dictionary.Add(dataField, text);
}
}
}
}

ASP.NET Client Id's not being generated for loaded control

I have a UserControl ('AddressInfoGroup') within an updatepanel that dynamically loads user controls ('AddressInfo') via the loadControl method. I have two other such controls on the same page, both of which function correctly.
When the AddressInfoGroup control renders an AddressInfo control to the page, the text boxes in AddressInfo have id's unchanged from the ascx markup. These id's should have been dynamically generated by the .NET clientID process. Because of this, when a second AddressInfo control is added to AddressInfoGroup, I get a runtime exception "An entry with the same key already exists". The two working controls produce proper clientID's and consequently don't return this error.
I should also mention that the AddressInfoGroup control has a few buttons which are correctly rendered with clientID so it seems to be an issue with the AddressInfo control itself. Any suggestions at all would be really helpful!
AddressInfoGroup markup
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="AddressInfoGroup.ascx.cs" Inherits="MFRI.Controls.Contact.AddressInfoGroup" %>
<h2>Address information</h2>
<div class="formGroup">
<asp:PlaceHolder ID="plc_infoCtrls" runat="server" />
<asp:Button id="btn_addAddress" CssClass="btn_add" Text="v" runat="server" OnClick="addAddress_click" UseSubmitBehavior="false" CausesValidation="false" />
<asp:Button id="btn_removeAddress" CssClass="btn_remove" Text="^" runat="server" OnClick="removeAddress_click" UseSubmitBehavior="false" CausesValidation="false" />
</div>
AddressInfoGroup codebehind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MFRI.Common.Contact;
using MFRI.Common;
namespace MFRI.Controls.Contact
{
public partial class AddressInfoGroup : System.Web.UI.UserControl
{
private int _maxNumberOfControls = 3;
private int _minNumberOfControls = 1;
private List<Address> _controlListToBind = null;
public int NumberOfControls
{
get { return SafeConvert.ToInt(ViewState["NumberOfControls"] ?? 0); }
set { ViewState["NumberOfControls"] = value; }
}
public bool Editable
{
get { return (bool)(ViewState["InfoControlsEditable"] ?? false); }
set { ViewState["InfoControlsEditable"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (_controlListToBind != null && _controlListToBind.Count > 0)
{
NumberOfControls = _controlListToBind.Count;
foreach (Address address in _controlListToBind)
{
AddressInfo addressInfo = (AddressInfo)LoadControl("AddressInfo.ascx");
addressInfo.InitControl(Editable, address);
plc_infoCtrls.Controls.Add(addressInfo);
}
}
else
{
if (NumberOfControls <= 0)
NumberOfControls = 1;
for (int i = 0; i < NumberOfControls; i++)
{
AddressInfo addressInfo = (AddressInfo)LoadControl("AddressInfo.ascx");
addressInfo.InitControl(Editable, null);
plc_infoCtrls.Controls.Add(addressInfo);
}
}
RefreshButtons();
}
public void BindAddressList(List<Address> addressList)
{
_controlListToBind = addressList;
}
public List<Address> GetAddressList()
{
List<Address> returnList = new List<Address>();
foreach (AddressInfo addressInfo in plc_infoCtrls.Controls.OfType<AddressInfo>())
{
returnList.Add(addressInfo.GetAddress());
}
return returnList;
}
private void RefreshButtons()
{
btn_addAddress.Enabled = false;
btn_removeAddress.Enabled = false;
if (Editable)
{
if (NumberOfControls > _minNumberOfControls)
btn_removeAddress.Enabled = true;
if (NumberOfControls < _maxNumberOfControls)
btn_addAddress.Enabled = true;
}
}
protected void addAddress_click(object sender, EventArgs e)
{
if (NumberOfControls < _maxNumberOfControls)
{
AddressInfo addressInfo = (AddressInfo)LoadControl("AddressInfo.ascx");
addressInfo.InitControl(Editable, null);
plc_infoCtrls.Controls.Add(addressInfo);
NumberOfControls++;
}
RefreshButtons();
}
protected void removeAddress_click(object sender, EventArgs e)
{
if (_minNumberOfControls < NumberOfControls)
{
plc_infoCtrls.Controls.RemoveAt(plc_infoCtrls.Controls.Count - 1);
NumberOfControls--;
}
RefreshButtons();
}
}
}
AddressInfo markup
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="AddressInfo.ascx.cs" Inherits="MFRI.Controls.Contact.AddressInfo" %>
<div>
<asp:Label ID="lbl_line1" AssociatedControlID="txt_line1" runat="server">Line 1:</asp:Label><asp:TextBox ID="txt_line1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="val_line1" runat="server" ErrorMessage="Please include a street address" ControlToValidate="txt_line1" Display="Dynamic">*</asp:RequiredFieldValidator>
</div>
<div>
<asp:Label ID="lbl_line2" AssociatedControlID="txt_line2" runat="server">Line 2:</asp:Label><asp:TextBox ID="txt_line2" runat="server"></asp:TextBox>
</div>
<div>
<asp:Label ID="lbl_zip" AssociatedControlID="txt_zip" runat="server">Zip code:</asp:Label><asp:TextBox ID="txt_zip" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="val_zip" runat="server" ErrorMessage="Please include a zip code" ControlToValidate="txt_zip" Display="Dynamic">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="regVal_zip" ControlToValidate="txt_zip" ErrorMessage="Zip code must be made up of integers in the format xxxxx" ValidationExpression="^\d{5}$" Runat="server" Display="Dynamic">*</asp:RegularExpressionValidator>
</div>
<div>
<asp:Label ID="lbl_city" AssociatedControlID="txt_city" runat="server">City:</asp:Label><asp:TextBox ID="txt_city" runat="server" CssClass="disabled"></asp:TextBox>
</div>
<div>
<asp:Label ID="lbl_state" AssociatedControlID="ddl_state" runat="server">State:</asp:Label><asp:DropDownList ID="ddl_state" runat="server" Enabled="false"></asp:DropDownList>
</div>
<div>
<asp:Label ID="lbl_edit" AssociatedControlID="chk_edit" runat="server">Edit City/State:</asp:Label><asp:CheckBox ID="chk_edit" runat="server" />
</div>
AddressInfo codebehind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MFRI.Common.Contact;
using System.Text;
using System.Globalization;
using System.Threading;
using MFRI.Common;
namespace MFRI.Controls.Contact
{
public partial class AddressInfo : System.Web.UI.UserControl
{
private bool _editable = false;
private bool _allowStateCityEdit = false;
protected bool AllowStateCityEdit{
set { _allowStateCityEdit = value; }
}
protected ClientScriptManager clientScriptManager
{
get { return Page.ClientScript; }
}
protected void Page_Load(object sender, EventArgs e)
{
txt_city.Attributes.Add("readonly", "readonly");
RegisterEditCityStateScript();
if (_editable)
RegisterZipCodeScript();
}
public void InitControl(bool editable, Address address)
{
InitStateDropDown();
if (!_allowStateCityEdit)
{
lbl_edit.Visible = false;
chk_edit.Visible = false;
}
_editable = editable;
if (!_editable)
{
txt_line1.Enabled = false;
val_line1.Enabled = false;
txt_line2.Enabled = false;
txt_city.Enabled = false;
txt_zip.Enabled = false;
val_zip.Enabled = false;
regVal_zip.Enabled = false;
ddl_state.Enabled = false;
chk_edit.Enabled = false;
}
else
{
txt_zip.Attributes.Add("onblur", "GetCityState('" + txt_city.ClientID + "','" + ddl_state.ClientID + "','" + txt_zip.ClientID + "');");
chk_edit.Attributes.Add("onClick", "ToggleCityState('" + txt_city.ClientID + "','" + ddl_state.ClientID + "','" + txt_zip.ClientID + "')");
}
if (address != null)
{
txt_line1.Text = address.Line1;
txt_line2.Text = address.Line2;
txt_city.Text = address.City;
txt_zip.Text = SafeConvert.ToString(address.Zip);
ddl_state.SelectedValue = SafeConvert.ToString((int)address.State);
}
}
private void RegisterZipCodeScript(){
if (!clientScriptManager.IsClientScriptBlockRegistered(this.GetType(), "zipCodeScript"))
{
StringBuilder script = new StringBuilder();
script.Append("function GetCityState(txtCityId,ddlStateId,txtZipId) {\n");
script.Append(" var textZip = document.getElementById(txtZipId);\n");
script.Append(" var zipCode = parseFloat(textZip.value);\n");
script.Append(" if(isNaN(zipCode)){\n");
script.Append(" var txtCity = document.getElementById(txtCityId);\n");
script.Append(" var ddlState = document.getElementById(ddlStateId);\n");
script.Append(" txtCity.value = '';\n");
script.Append(" ddlState.selectedIndex = 0;\n");
script.Append(" }\n");
script.Append(" else{\n");
script.Append(" MFRI.Controls.Contact.ZipCodeService.GetCityState(zipCode, txtCityId, ddlStateId, SucceededCallback);\n");
script.Append(" }\n");
script.Append("}\n");
script.Append("function SucceededCallback(result) {\n");
script.Append(" var txtCity = document.getElementById(result[0]);\n");
script.Append(" txtCity.value = result[2];\n");
script.Append(" var ddlState = document.getElementById(result[1]);\n");
script.Append(" var stateId = result[3];\n");
script.Append(" for(i=0; i<ddlState.options.length; i++){\n");
script.Append(" if(ddlState.options[i].value == stateId){\n");
script.Append(" ddlState.selectedIndex = i;\n");
script.Append(" }\n");
script.Append(" }\n");
script.Append("}\n");
clientScriptManager.RegisterClientScriptBlock(this.GetType(), "zipCodeScript", script.ToString(), true);
}
}
private void RegisterEditCityStateScript()
{
if (!clientScriptManager.IsClientScriptBlockRegistered(this.GetType(), "editCityState"))
{
StringBuilder script = new StringBuilder();
script.Append("function ToggleCityState(txtCityId, ddlStateId, txtZipId) {\n");
script.Append(" var txtCity = document.getElementById(txtCityId);\n");
script.Append(" var ddlState = document.getElementById(ddlStateId);\n");
script.Append(" if(ddlState.disabled == true){\n");
script.Append(" txtCity.removeAttribute('readonly');\n");
script.Append(" ddlState.disabled = false;\n");
script.Append(" }\n");
script.Append(" else{\n");
script.Append(" txtCity.setAttribute('readonly', 'readonly');\n");
script.Append(" ddlState.disabled = true;\n");
script.Append(" GetCityState(txtCityId,ddlStateId,txtZipId); \n");
script.Append(" }\n");
script.Append("}\n");
clientScriptManager.RegisterClientScriptBlock(this.GetType(), "editCityState", script.ToString(), true);
}
}
private void InitStateDropDown(){
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
ddl_state.Items.Add(new ListItem("Select a state","-1"));
foreach (StateType state in Enum.GetValues(typeof(StateType)))
{
string displayName = state.ToString().ToLower();
displayName = displayName.Replace('_', ' ');
ddl_state.Items.Add(new ListItem(textInfo.ToTitleCase(displayName), state.GetHashCode().ToString()));
}
}
public Address GetAddress()
{
Address address = new Address();
address.Line1 = txt_line1.Text;
address.Line2 = txt_line2.Text;
address.State = (StateType)Enum.ToObject(typeof(StateType), SafeConvert.ToInt(ddl_state.SelectedValue));
address.City = txt_city.Text;
address.Zip = SafeConvert.ToInt(txt_zip.Text);
return address;
}
}
}
I figured it out finally.
Each addressInfo Control is created on the fly by addressInfoGroup. After instantiation, addressInfoGroup calls the 'initControl' method of the newly created addressInfo control. Within that init function I retrieve a few clientID values:
txt_zip.Attributes.Add("onblur", "GetCityState('" + txt_city.ClientID + "','" + ddl_state.ClientID + "','" + txt_zip.ClientID + "');");
chk_edit.Attributes.Add("onClick", "ToggleCityState('" + txt_city.ClientID + "','" + ddl_state.ClientID + "','" + txt_zip.ClientID + "')");
Because of my reference to the clientID's, all of the controls within addressInfo retain the markup Id's. If I comment those lines out, .NET generates the proper Id's. This has something to do with WHEN these lines are called. When I moved the lines into addressInfo's postback, everything worked as expected.
So end result: If a parent control tells a child control to reference a clientID, all of the child controls clientID's seem to malfunction.
Have you thought about using a ListView control to dynamically create and display your Address controls on the fly? You would have one instance of the control hard-coded on the page and then in your ListView's ItemTemplate you would put in your Address control and databind it to the data in your ListView's DataSource property.
Have you tried explicitly setting the ID value of the dynamically added AddressInfo controls? For instance, in the Page_Load of the AddressInfoGroup:
protected void Page_Load(object sender, EventArgs e)
{
if (_controlListToBind != null && _controlListToBind.Count > 0)
{
NumberOfControls = _controlListToBind.Count;
int index = 0;
foreach (Address address in _controlListToBind)
{
AddressInfo addressInfo = (AddressInfo)LoadControl("AddressInfo.ascx");
addressInfo.ID = String.Format("AddressInfo{0}", index++);
addressInfo.InitControl(Editable, address);
plc_infoCtrls.Controls.Add(addressInfo);
}
}
else
{
if (NumberOfControls <= 0)
NumberOfControls = 1;
for (int i = 0; i < NumberOfControls; i++)
{
AddressInfo addressInfo = (AddressInfo)LoadControl("AddressInfo.ascx");
addressInfo.ID = String.Format("AddressInfo{0}", i);
addressInfo.InitControl(Editable, null);
plc_infoCtrls.Controls.Add(addressInfo);
}
}
RefreshButtons();
}

Categories

Resources