Adding Events / Page_Load / Designer Removing it

  • Thread starter Thread starter Patrick
  • Start date Start date
P

Patrick

Hello

I have the following problem. I have an aspx Page. This page contains an
ASP-Table Object. So in that table, I have a linkbutton. So, I can't edit
the event of that button trough the WYSIWYG editor, so I co to the
CodeBehind and add the Event in the InitializeComponent() Method like this
this.lbTest.Click += new System.EventHandler(this.lbTest_Click);

so far, all works fine. now if i go to the WYSIWYG editor, add another
component to the page, that event will be removed and I have to add it
again.

Anyone an idea how I can force VS.NET not to remove that event??

Thanks

Patrick
 
Its a bad idea to add code to InitializeComponent as it is
being generated by VS.NET and will get rewritten if you
make any change in the Visual Designer.

You can add Click Events by either Double Clicking your
Link Button in the Designer view or go to Properties
Window for that Link Button and click on Events Icon. Now
give a name to the Click Event of that button.

Thanks,
-Shan
 
here an example what i mean

<asp:table runat=server>
<asp:tablerow runat="server">
<asp:tablecell runat="server">
<asp:linkbutton runat=server id=lbTest>Test</asp:linkbutton>
</asp:tablecell>
</asp:tablerow>
</asp:table>

now try to add the event to the linkbutton using the Visual Designer :-)
when I try to click the button, it activates the table and adds the events
for the tables ... but how can i access that button? :-)
 
Interesting!

I was under the impression we were talking of HTML Table
Tags ! Sorry for the confusion.

I did look into it and yes there was no way to access the
controls!!It only allows us to add the Table Events in
Designer View...

I tried to add rows/cols programatically and it worked !

So I did some research and this is what MSDN say on the
Table Control ..

****
The Table control allows you to build an HTML table and
specify its characteristics in a straightforward manner. A
table can be built at design time given some static
content, but the power of a Table Web server control is
often realized when the table is built programmatically
with dynamic contents.
****

Source : http://msdn.microsoft.com/library/default.asp?
url=/library/en-
us/cpref/html/frlrfsystemwebuiwebcontrolstablememberstopic.
asp

As mentioned above Table Control is more powerful when it
is built programatically.

This is wat I did ... I placed a Table control in the form
and then programatically added rows/cols and a Button
Control ! I was able to write a Click Event for the button
control and associate the Event with the Button and was
able to access it and it did work!

Here is a gist of wat I did,

private void Page_Load(object sender, System.EventArgs e)
{
TrTest = new TableRow();
TcTest = new TableCell();
btnTest = new Button();
TcTest.Controls.Add(btnTest);
TrTest.Cells.Add(TcTest);
TblTest.Rows.Add(TrTest);
btnTest.ID = "Test";
btnTest.Text = "Test Button";
btnTest.Click += new System.EventHandler
(this.btnTest_Click);
}

private void btnTest_Click(object sender,System.EventArgs
e)
{
Response.Write("It Works!");
}


HTH

Thanks,
-Shan
 
yes, i know that that works like this. But same Happens with Placeholders
etc :-) so it's not only a table-problem. but I found a solution - I add my
events to the OnInit-Code. The Visual Designer does not modify that one. so
it works now :-)

Patrick
 
Back
Top