 |
| C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 6th, 2010, 02:35 AM
|
|
Authorized User
|
|
Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
Problem with Passing parameters in Dynamic Control
Hi,
i just want to pass a variable containing a string to a Method called 'upd_Click' in a Click event of the Dynamically created Button Control.
I am getting a Error "Method name expected".
Can somebody help?
private void Page_Load(object sender, System.EventArgs e)
{
string s;
Button upd = new Button();
upd.ID = "update";
upd.Text= "update";
s = "hello";
form1.Controls.Add(upd);
upd.Click += new EventHandler(upd_Click(s));//getting Error in this line
}
void upd_Click(string str)
{
System.Windows.Forms.MessageBox.Show(str);
}
|
|

April 6th, 2010, 03:13 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
You can't do that, as the method you pass to the EventHandler must have the correct method signature, i.e. have two parameters, the first being an object, and the second a EventArgs.
Perhaps if you tell us what you are trying to accomplish and we might be able to help you.
|
|

April 6th, 2010, 01:09 PM
|
|
Authorized User
|
|
Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
I am creating two Dynamic Panels containing two textboxes and two update Buttons in a Page_Load event.
-I just want to type some string in One textbox From a Panal at Run-time.
-And when i click the corresponding 'update' Button, i want to display the string i entered in the corresponding textbox to display in the MessageBox During update Button Click Event.
-I also tried this using FindControl in 'upd_Click' Method. But since it has two Panel, it coun't find its Id.
This is my code:
private void Page_Load(object sender, System.EventArgs e)
{
string s;
for (int i = 1; i <= 2; i++)
{
Panel p = new Panel();
p.ID = "p" + i.ToString();
p.BackColor = Color.BlueViolet;
p.Height = Unit.Pixel(300);
p.Width = Unit.Pixel(300);
Button up = new Button();
up.ID = "B" + i.ToString();
up.Text = "update";
TextBox t = new TextBox();
t.ID = "t" + i.ToString();
t.Text = "hello";
s = t.Text;
up.CausesValidation = true;
up.CommandName = "Update";
p.Controls.Add(t);
p.Controls.Add(up);
Form.Controls.Add(p);
up.Click += new System.EventHandler(upd_Click(i, s));
}
}
public static void upd_Click(int j,string str)
{
System.Windows.Forms.MessageBox.Show("j: " + j+"<br>"+"str: "+str);
}
|
|

April 6th, 2010, 01:15 PM
|
|
Authorized User
|
|
Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
Sorry. I want to Mention one more thing.
- The reason i gave a string already in a variable s="hello" because i cannot pass by value to a Method without initializing it.
-But actually i want to enter a string during run-time.
How can i do that?
|
|

April 6th, 2010, 01:26 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Well, for starters you can't use MessageBox in a web form.
Then when you create an event handler you aren't passing in a method call, you are simply passing in a method name. So you can't put "upd_Click(i,s)", you can only put in "upd_Click". But upd_Click MUST have two parameters, the first a object, and the second an Event Handler.
I don't know exactly what you are trying to do but the following might help:
Code:
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 2; i++)
{
Panel p = new Panel();
p.ID = "P" + i;
p.Height = Unit.Pixel(300);
p.Width = Unit.Pixel(300);
Button b = new Button();
b.ID = "B" + i;
b.Text = "Update";
b.Click += new EventHandler(Button_Click);
TextBox t = new TextBox();
t.ID = "T" + i;
p.Controls.Add(b);
p.Controls.Add(t);
Form.Controls.Add(p);
}
}
void Button_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
TextBox t0 = (TextBox)FindControl("T0");
TextBox t1 = (TextBox)FindControl("T1");
t1.Text = t0.Text;
}
|
|

April 6th, 2010, 02:08 PM
|
|
Authorized User
|
|
Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
Hi,
Thanks its working. But you have specified this code below in the Click event only if there is two textbox.
TextBox t0 = (TextBox)FindControl("T0");
TextBox t1 = (TextBox)FindControl("T1");
-Actually in the production, i want to dynamically create textboxes based on the database records. so if there is 25 records, for each record, i dynamically create 25 textboxes.
-So how will i use 'FindControl' in that case?
-And one more thing. the Message box is working inthe web form if i comment the 'upd_Click' line.
|
|

April 6th, 2010, 04:39 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Well as you saw in my example you can get the Button by casting the sender parameter.
Button b = (Button)sender;
You could then use this to get the corresponding TextBox a number of ways. Either study its ID and then calculate the ID of the TextBox you want. Or perhaps use b.Parent to get the Panel, and then use the Controls array to find the TextBox:
Panel p = (Panel)b.Parent;
TextBox t = (TextBox)p.Controls[1]; // The second control will be the TextBox.
The MessageBox command might work when you are running the web server and the browser on the same machine - but when the web server is on a different box to the browser the message box would appear on that machine, and there would be no-one there to click the OK button!
I'd recommend placing a Label control on your form somewhere that you can then set to display a value.
|
|

April 6th, 2010, 04:49 PM
|
|
Authorized User
|
|
Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
Thanks a lot. I'le try that.
|
|

April 7th, 2010, 01:03 PM
|
|
Authorized User
|
|
Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
Thank you so much. It worked.
-I have one more issue with the visibility of the Dynamic Panel control 'pan1'.
-I have a Dynamic Button control, and if i hover my mouse on it, Dynamic Panel control popsup.
- if i create everything in a design time its working, the issue is only with Dynamic.
-Here is my codes.
i just attached from my cssClass, the following code asusual in .aspx page.
<link href="AnimSheet.css"...
"AnimSheet.css" contains:
.popupMenu
{
height: 400px;
width: 400px;
visibility:hidden;
background-color: #800000;
}
.popupMenu:hover
{
visibility: visible;
}
So in "Default.aspx.cs": i added
pan1.CssClass = "popupMenu";
But it wasn't working.The Panel itself is hidden even when i hover on it. Please help.
|
|

April 7th, 2010, 01:10 PM
|
|
Authorized User
|
|
Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
Sorry. i think i have to be more clear.
-The issue is The Dynamic Panel control is visible FOR A SECOND in a Page_load event and goes away. and when i hover on the Button control its working.
-But i want the Panel control to popup only when i hover on the Button, not in Page_load event.
i think its because in my code i added:
Panel pan1=new Panel();
pan1.cssClass="popupMenu";
Form.Controls.Add(pan1);
|
|
 |