Set Server Side OnClick() event Programmatically - onclick

I am looking for a way to programmatically set the OnClick event handler for a TableCell object. The ASP equivalent of what I'm trying to do will look like this:
<asp:TableCell OnClick="clickHandler" runat="server">Click Me!</asp:TableCell>
In the above example, "clickHandler" is a server-side function defined in the .cs CodeBehind.
public virtual void clickHandler(object sender, EventArgs args) {...}
However, for my situation, this TableCell object needs to be created dynamically, so setting it in an ASP tag is not an option. I am trying to do something like the following in the CodeBehind:
System.Web.UI.WebControls.TableRow row = new System.Web.UI.WebControls.TableRow();
System.Web.UI.WebControls.TableCell cell = new System.Web.UI.WebControls.TableCell();
cell.Text = "Click Me!";
cell.Attributes.Add("onClick", "clickHandler");
row.Cells.Add(cell);
Unfortunately, in this situation:
cell.Attributes.Add("onClick", "clickHandler");
the "clickHandler" only works as a client-side javascript function. What I'm looking for is a way to link the server-side clickHandler() function, defined in the .cs CodeBehind, to this table cell.
After an afternoon of searching, I have been unable to come up with a working solution. Thanks in advance for any help.

After a lot of work and research, I was able to cobble together a working solution, but it seems like an awful lot of work for something that should already be built-in. What I did was to extend the System.Web.UI.WebControls.TableCell object to include a handle for the OnClick event:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyWebApp
{
public class ExpandableTableCell : TableCell, IPostBackEventHandler, INamingContainer
{
private static readonly object click_event = new object();
public ExpandableTableCell()
{
}
// public handles for adding and removing functions to be called on the click event
public event EventHandler Click
{
add
{
Events.AddHandler(click_event, value);
}
remove
{
Events.RemoveHandler(click_event, value);
}
}
// define parent function that will be called when the container is clicked
protected void Click(EventArgs e)
{
EventHandler h = Events[click_event] as EventHandler;
if (h != null)
{
h(this, e);
}
}
// specify the "post back event reference" or id of the click event
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
writer.AddAttribute(HtmlTextWriterAttribute.Onclick,
Page.ClientScript.GetPostBackEventReference(this, "custom_click"));
}
// link the custom click id to the click function
void System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
{
if(eventArgument == "custom_click")
{
this.OnClick(EventArgs.Empty);
}
}
}
}
Here is how I use my new class (almost exactly like the stock TableCell):
System.Web.UI.WebControls.TableRow row = new System.Web.UI.WebControls.TableRow();
ExpandableTableCell click_cell = new ExpandableTableCell();
click_cell.Text = "Click Me!";
click_cell.Click += clickHandler;
// extra little touch for mouseover event
click_cell.Attributes.Add("onmouseover", "this.style.cursor='pointer'");
row.Cells.Add(click_cell);
As I have said, it seems like going through the trouble of extending the class to set the OnClick method in the codebehind is excessive. If anyone has any other ideas or any ways to clean up or legitimize the code above, please let me know.

I don't know if this is relevant to your problem, but I was trying to add a server-side function to a LinkButton and found the following (VB) code: AddHandler cell.Click, AddressOf clickHandler, which worked for me.
According to this code conversion service, this translates to cell.Click += clickHandler; in C#.
Hope this helps!

Related

C# ASP.NET - Controlling/updating a textbox.text value through a class

Newbie here, I need help with a website I'm creating.
I have a class that does some analysis on some text that is input by the user, the class then finds an appropriate answer and sends it back to the textbox. (in theory)
Problem is I don't know how I can control and access the textbox on the default.aspx page from a class, all I get is "object reference is required non static field".
I made the textbox public in the designer file yet still no joy. :(
I've also read this: How can I access the controls on my ASP.NET page from a class within the solution? , which I think is along the lines of what I'm trying to achieve but I need clarification/step by step on how to achieve this.
Hope someone can point me in the right direction.
Many thanks,
Kal
This is the code I have added to the designer.cs file:
public global::System.Web.UI.WebControls.TextBox TextBox3;
public string MyTextBoxText
{
get
{
return TextBox3.Text;
}
set
{
TextBox3.Text = value;
}
}
This is the class method i have created:
public static cleanseMe(string input)
{
string utterance = input;
string cleansedUtt = Regex.Replace(utterance, #"[!]|[.]|[?]|[,]|[']", "");
WebApplication1._Default.TextBox3.text = cleansedUtt;
}
I could just return the cleansedUtt string i know, but is it possible for me to just append this string to the said textbox from this method, within this class?
I also tried it this way, i wrote a class that takes in the name of the textbox and string to append to that textbox. it works BUT only on the default.aspx page and does not recognise the textbox names within the difference classes. The code is as follows:
public class formControl
{
public static void ModifyText(TextBox textBox, string appendthis)
{
textBox.Text += appendthis + "\r\n";
}
I would suggest you that do not access the Page Controls like TextBox in your class. It will be more useful and a good practice that whatever functionality your class does, convert them into function which accept the parameters and returns some value and then on the basis of that value you can set the controls value.
So now you have reusable function that you can use from any of the page you want. You do not need to write it for every textbox.
Here I am giving you a simple example
public class Test
{
public bool IsValid(string value)
{
// Your logic
return true;
}
}
Now you can use it simple on your page like this
Test objTest = new Test();
bool result=objTest.IsValid(TextBox1.Text);
if(result)
{
TextBox1.Text="Everything is correct";
}
else
{
TextBox1.Text="Something went wrong";
}
If you have your class in the same project (Web Project) the following will work:
public class Test
{
public Test()
{
//
// TODO: Add constructor logic here
//
}
public static void ValidateTextBox(System.Web.UI.WebControls.TextBox txt)
{
//validation logic here
if (txt != null)
txt.Text = "Modified from class";
}
}
You can use this from your webform like this:
protected void Page_Load(object sender, EventArgs e)
{
Test.ValidateTextBox(this.txt);
}
If your class is in a different (class project), you would need to add a reference to System.Web to your project.

what will happen if we don't call base.createchildcontrols()

I just would like to know what will happen if we don't put base.createchildcontrols() in the code. Will composite control be created without calling base.createchildcontrols()?
[ToolboxData("<{0}:Login runat=server></{0}:Login>")]
public class Login : CompositeControl
{
private TextBox txtUsername = new TextBox();
private TextBox txtPassword = new TextBox();
private Button btnLogin = new Button();
protected override void CreateChildControls()
{
txtUsername.ID = "txtUsername";
txtPassword.ID = "txtPassword";
txtPassword.TextMode = TextBoxMode.Password;
btnLogin.ID = "btnLogin";
btnLogin.Text = "Login";
Controls.Add(txtUsername);
Controls.Add(txtPassword);
Controls.Add(btnLogin);
base.CreateChildControls();
}
}
The short answer is... Nothing! You don't need to call the base implementation (although you can always try removing it to see what happens ;-)
Using ILSpy, we can see that CompositeControl inherits from WebControl which inherits from Control.
CreateChildControl() is defined on Control as:
protected internal virtual void CreateChildControls()
{
}
i.e. It is only there to be overriden.
Compare this with some other controls that inherit from Control, like BaseDataList and you can see that that method has a lot of functionality for checking and rendering the output.
This makes sense. Reading the MSDN documentation, here, we can see that it is for you to implement the rendering of any child controls. Only if the class you are inhereting from requires this method to be called, then you'll have to call it.

ASP. A list and pictures

I put a list box and an image box.
now I want the image to swap every time the user clicks on a different element in the list. It doesnt seem to work
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
string[] pictures = { "~/createWii.jpg",
"~/DKC4_wii.png",
"~/Donkey-Kong-Country-1.jpg",
"~/DSCallOfDutyBlackOps.jpg",
"~/DSPreviewsCodmw2.jpg",
"~/DSPreviewsAliceInWonderLAnds.jpg",
"~/DSPreviewPicross3d.jpg",
"~/createii.jpg",
};
string[] picturesNames = { "picture1", "picture2", "picture3", "picture4", "picture5", "picture6", "picture7", "picture8" };
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < pictures.Length; i++)
{
ListBox1.Items.Add(new ListItem(picturesNames[i],pictures[i]));
}
Image1.ImageUrl = "~/Donkey-Kong-Country-1.jpg";
ListBox1.DataSource = picturesNames;
ListBox1.DataBind();
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Image1.ImageUrl = pictures[ListBox1.SelectedIndex];// it tells me that there is index out of range each time. why ?
}
}
A couple of things.
1.) you should wrap the code in page_load with
if(!IsPostback)
2.) Make sure on the .aspx that the "AutoPostback" property is set to true on the listbox!
Edit
Per the request in the comments, the reason this is needed is two fold.
ASP.NET ViewState will handle the persistence of the values on postback, therefore, you can use the !IsPostback condition to ensure that the information is only bound once. This prevents any "oddities" from coming up in the future.
By default ListBoxes/DropDownLists/etc do not post back automatically when the user changes a selection. So to actually trigger the event you either need to have a button that does the postback, or update the "AutoPostback" property as I directed to ensure that when the user makes a change that it triggers the server-side code.

System.Windows.Forms.WebBrowser inside Asp.net WebForm DocumentCompleted not fired

Yeah some people would say "Are you crazy using winforms controls inside asp forms"... and I think they are right. But I would say.. "I'm not the only one!!, take a look"
http://www.eggheadcafe.com/tutorials/aspnet/b7cce396-e2b3-42d7-9571-cdc4eb38f3c1/build-a-selfcaching-asp.aspx
So...
Doing some kind of stuff like the previous link. I did the following:
using System;
using System.Threading;
using System.Windows.Forms;
namespace XXXX.aspx.Print
{
public partial class Drucker : System.Web.UI.Page
{
private ManualResetEvent mre = new ManualResetEvent(false);
protected void Page_Load(object sender, EventArgs e)
{
Threading();
}
private void Threading()
{
Thread t = new Thread(new ThreadStart(GoAhead));
t.SetApartmentState(ApartmentState.STA);
t.Start();
mre.WaitOne();
t.Abort();
}
private void GoAhead()
{
DateTime time = DateTime.Now;
WebBrowser webBrowser = new WebBrowser();
webBrowser.Navigate(Request.UrlReferrer.ToString());
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
while (true)
{
Thread.Sleep(0);
TimeSpan elapsedTime = DateTime.Now - time;
if (elapsedTime.Seconds >= 13)
{
mre.Set();
}
System.Windows.Forms.Application.DoEvents();
}
}
void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser webBrowser = (WebBrowser)sender;
if (e.Url.AbsolutePath != webBrowser.Url.AbsolutePath) return;
webBrowser.Print();
}
}
}
Now...
the DocumentCompleted event is not fired (neither ProgressChanged) and I've tried the next:
Add the library MsHtml.dll to my project and place the file into my lib folder... I did it... No changes.
Try to handle the state of the WebBrowser.ReadyState... I did it... No changes (Actually after receive the WebBrowserReadyState.Complete I tried to print the document with
webBrowser.Print(); but I receive a weird IE pop up telling me: "'dialogArguments.__IE_PrintType' is Null or a not an object'"... ok so doing some research I got: a microsoft topic about dcomcnfg and some com security settings
I did it... No changes.
...
By the way I´m working in 64bits, Win7...
So before I format the whole computer...
Any suggestions?
WebBrowser is never inplace-activated by its ActiveX container, which you don't have. Try put it on a form first.
By the way, you know you are in the unsupported territory when you use WinInet, the network layer of webbrowser control, in a Windows Service, right?

Pass data to user components in asp.net

It is .net 2.0 here, not MVC, and I am crap at asp forms.
I have a page with user controls on it. When I click on something in the page, I want to load the usercontrol based on a parameter from the page.
I cannot do it.
In my page's FaultTree_Clicked, I get the value, then:
I tried exposing a property on the
child user control to set the value, which i set in FaultTree_Clicked,
it gets forgotten.
I tried saving it to
Session["mykey"], and loading
Session["mykey"] in the control's
Page_init... the value is blank.
I tried saving it to
ViewState["mykey"], and loading
ViewState["mykey"] in the control's
Page_init... the value is blank.
EDIT: more specific info:
Here is a cut down version of what the page(MyFault) looks like:
<form id="form" runat="server">
<div id="faulttree">
<asp:TreeView ID="FaultTree" ......>
</div>
<uc1:_DefectDetail ID="DefectDetail" runat="server" Visible="true" EnableViewState="true" />
</form>
And there is a method on the pages code behind "FaultTree_SelectedNodeChanged()".
When that method is hit, I want to load/show the DefectDetail control. The DefectControl requires a faultid, which comes off the Tree, which I successfully get in the SelectedNodeChanged method. I cannot get the faultid into the defect control.
This has to do with ASP.NET page lifecycle. By the time the click event fires, the control's init event has already happened.
In order to better assist you, please provide a more detailed explanation of what the FaultTree control is, what is the desired result and some sample code.
UPDATE:
Instead of a public property, you can simply create a public method in the control that does the desired action and invoke it from the FaultTree_SelectedNodeChangeEvent.
Example (for a public method named Refresh):
_DefectDetail.Refresh(object data);
Basically you have to use EventHandlers....
1. Add a event handler to your user control (I had a search bar UscSearchCriteriaBar1)
public event EventHandler CriteriaChanged;
+
private void InternalOnCriteriaChanged()
{
OnCriteriaChanged();
}
+
protected virtual void OnCriteriaChanged()
{
// If there are registered clients raise event
if (CriteriaChanged != null)
CriteriaChanged(this, EventArgs.Empty);
}
+
Example
public int EmployeeID
{
get
{
f (Session["EmployeeID"] != null)
{
ViewState["EmployeeID"] = Convert.ToInt32(Session["EmployeeID"]);
}
if (ViewState["EmployeeID"] == null)
ViewState["EmployeeID"] = 0;
return int.Parse(ViewState["EmployeeID"].ToString());
}
set
{
ctlEmployee.SelectedValue = value.ToString();
ViewState["EmployeeID"] = value;
Session["EmployeeID"] = value;
}
}
In your page or other control
override protected void OnInit(EventArgs e)
{
InitializeComponent();
UscSearchCriteriaBar1.CriteriaChanged += new EventHandler(this.CriteriaChanged);
base.OnInit(e);
}
private void CriteriaChanged(object sender, EventArgs e)
{
try
{
RefreshData();
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
}
}
You can get UscSearchCriteriaBar1.EmployeeID
This code should give you some ideas...was done for 1.1 should work on 2.

Resources