Call function from ascx to .cs of a user control and draw html - ascx

I have a user control, I want to make if conditions in the ascx, and upon that call a function in the user control and return data from the method to be displayed as html in the ascx. Any ideas? I am still newbie to asp.

Just drop a literal onto your user control. Then in your code behind, it'll look something like this:
private void Page_Load(object sender, System.EventArgs e)
{
// Make your IF conditions here
MyFunction();
}
private void MyFunction()
{
string myHtml = "<div>My Html Code!</div>";
literal1.Text = myHtml;
}
That should render your HTML.

Related

How can i open form in class with C#?

I want to write class codes to open form. How can I make changes the codes to work?
My Class Codes:
public void formac(Form frm, Form formName)
{
formName frm = new formName();
frm.Show();
}
Button Click Codes:
openFormClass MyClass = new openFormClass();
MyClass.formac(frm,Form1);
Hello and welcome to StackOverflow!
Unfortunately your question is not very concise on what you want to achieve. Maybe have a look at How do I ask a good question?.
From what you've shown, I'm guessing that you're just want to show another instance of Form1 on a button click. Then the following should be sufficient:
private void button1_Click(object sender, System.EventArgs e)
{
var frm = new Form1();
frm.Show();
}
i want to write code to open form in class (parameter will be "Form Name")

WicketTester for page update: assert locale change

I'm changing the sites locale with an AjaxLink:
#Override
public void onClick(AjaxRequestTarget target) {
Session.get().setLocale(newLanguage.getLocale());
// Add whole page to update instead of single components
target.add(getPage());
}
It does work as i expect, every i18n string on the entire page gets updated while form contents are kept. Splendid.
But testing with WicketTester won't work out. Both methods, clickLink and executeAjaxBehavior, do trigger the AjaxLink, but WicketTester doesn't detect any changes for the model value.
#Test
public void check() {
tester.startPage(SwitchLangPage.class);
tester.clickLink("link", true);
tester.assertModelValue("link:label", "English");
}
Am I missing something importent here?
(Wicket 6.19)
Edit: Gist with a simplified panel

TabControl avoid changing to another tab

I have a tab control with 2 tabs.. When the user clicks on the second tab, it does some validation and then if that validation returns false, the user gets a message indicating to go back. Now, here's my problem, it changes tabs anyways with the code below:
Although the user doesn't see the tab 2, it is showing as changed.
private void tabprincipal_SelectedIndexChanged(object sender, EventArgs e)
{
if (!saved_plan)
{
MessageBox.Show("You need to save a plan first.");
return;
}
How can I avoid this behavior? I want to display the message and the user to remain in the first tab
I think I'm looking for an event prior to the selectedindexchanged to detect that the user clicked tab2 and then don't let him move..
I actually found a way using the Deselecting method of the TabControl
private void tabprincipal_Deselecting(object sender, TabControlCancelEventArgs e)
{
if (!saved_plan)
{
MessageBox.Show("You need to save a plan first");
e.Cancel = true;
}
}

Enable/disable an ordinary button on Form2 from Form1

I need some help. I've been reading this site for days, and have read a lot of tips about controlling for example a button's property from another form. There's even a video on Youtube, which works to me as stand alone, but when I implenet it in my application it throws a NullReferenceException.
Let's say that I have a toolstrip menu on Form1. A click on the Kalibracio option opens the second form (also called Kalibracio - not Form2). Then, click on Proba in the menu should disable an ordinary button on the Kalibracio form which propery is set to public. The code on Form1 is as follows:
private void kalibracioToolStripMenuItem_Click(object sender, EventArgs e)
{
Kalibracio Kalibr = new Kalibracio(this);
Kalibr.Owner = this;
Kalibr.Show();
}
private void probaToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Application.OpenForms.OfType<Kalibracio>().Any())
(this.Owner as Kalibracio).button1.Enabled = false;
// the above line throws a NullReferenceExcteption if Kalibracio form is open (Kalibracio is null)
}
What am I missing?
LoL just had to declare globaly an instance of Kalibracio, open it and then access it's properties from all other methods.
I tried this approach at first, but my problem was that I was creating and instance locally, then I had to create another one in some other method because I couldn't address the former one created locally, and ofc it didn't work...

How to access child controls inside inherited class?

Here is what I'm doing, I have a large amount of reports, rather than copy and pasting the repeated code, I have the following..
public class ReportPage : System.Web.UI.Page
{
// Code
}
My report Pages have
public class MyReportPage : ReportPage
{
}
Problem I'm having is, I want to be able to access a control inside MyReportPage within ReportPage, for instance, I want all pages grid to have a specific property.
Inside ReportPage I tried
protected void Page_PreRender(object s, EventArgs e)
{
var obj = this.FindControl("reportGridName");
}
But unable to find the control, went through all the controls to see if I could it in, no luck..
Any ideas how to get the grid access on the inherit?
Page page = (Page)HttpContext.Current.Handler;
var obj = page.FindControl("reportGridName");
Does that work? I'm having the exact same problem and stumbled upon this link