Monday, March 12, 2012

How to handle events?

Sirs,

How can I handle events of controls created during runtime?
Let us say if I have nested repeaters how can I handle the events coming from the "child" repeaters? Or if I create a command button from the code how can I handle the events coming out of them? Example:


dim cmdButton as new button
cmdbutton.text="Hello"

page.controls.add(cmdbutton)

Now how to handle the click on the cmdbutton?

Regards,

TeddyCome on, Guys be more creative... I really need the answer...

Thx,
Teddy
I'm actually looking for the samething (in asp.net 2)

I've tried using:

.CommandName = "Method";
.Command += new CommandEventHandler(Method);

I don't get errors, just the event doesn't fire.

.OnCommand throws permisiion error

Labels: , , , , , , , , ,

How to handle events for controls within templates?

I have a FormView control, containing an EditItemTemplate with a dropdown listbox. I want to handle the SelectedIndexChanged event so I can hide or show part of the form...

If I double-click the dropdown to create the event handler, I get the function dropdown1_SelectedIndexChanged()... but without " handles Dropdown1.SelectedIndexChanged"

Obviously the dropdown1 control is inside the EditItemTemplate - but it still generates events? how does one get these?

This is getting frustrating: every time I try to design stuff logically in a componentised way, I get kicked in the teeth with a "gotcha". It was easier using classic ASP and just throwing the html out using Response.Write!!

Hello.

I think that you're using vb.net, right? in c# I don't see any trouble with this. Doesn't something like this work in vb.net:

<asp:DropDownListrunat="server"id="T"OnSelectedIndexChanged="p"AutoPostBack="true">

<asp:ListItemText="1"/>

<asp:ListItemText="2"/>

</asp:DropDownList>

then just define the p method without using the handles (but with the normal parameter list).


Ah, many many hanks: I didn't realise thats how it worked (instead of event-based handling). It so frustrated me last night i turned off the PC and polished off a bottle of wine.

That part works at last!

Now all I need to do is figure out how to control my datasource and formview to tell them to open in edit or insert mode by default (and not select).

Labels: , , , , , , , , , , , , , ,

How to handle errors when using GridView and DetailsView without writing any code

It is great that no code is neccessory when using GridView and DetailsView controls together with a SqlDataSource control. Since there is no code, "try.. catch.. finally" can not be used (nowhere to place the code block). I am wondering how the errors can be trapped and displayed. We certainly do not want users to see a generic .net error page. Can anyone give me some information on this? thanks,

Tom

I guess no one has noticed this question. Let me rephrase it. I really need some help on this. How can you use "try...catch..finally" to handle errors when working with GridView, DetailsView and SqlDataSource controls without writing any code? Or how and where to add error handling code? Thanks,
This is somewhat of a general answer to your question and it may not be of any help. You could use try-catch blocks that will run when events fired by your aspx page like Page_Load, On_Selecting etc... That way these error catching mechanisms will be in place to handle any abnormal behavior your code might produce during runtime.

Thanks plazma, for the suggestion. In fact, I had thought about the way you suggested. It did not work out (or at least I could not figure out how it works). The problem is this. Noramally, the data retrieval and data binding happen inside try.. catch block.

try{
//contact database and bind data to data controls
}
catch{
//handle error
}

However, for the codeless programming, data retrieval and data binding are done automatically by the controls. If the try..catch block is placed in Page_Load or other locations, it could catch some errors but they would not be the data retrieval and databinding related. If let's say there is a permission issue in database, this try..catch block will not be able to catch it because the data retrieval and databinding do not happen inside the block.


I found out that error can be handled in the events (selected, inserted and updated) of the SqlDataSource control:
protected void MySqlDataSource_Selected(object sender, SqlDataSourceStatusEventArgs e){ if (e.Exception != null) { // Mask theerror with a generic message (for security purposes.) lblError.Text = "An exception occurred performing the query."; // Consider theerror handled. e.ExceptionHandled = true; }}

Labels: , , , , , , , , , , , ,