|
|
Aside from dealing with UI events (such as OnClick for Buttons) and getting at DataItems in bound controls (such as OnItemDataBound for Repeaters), I'm having a difficult time understanding why one would implement event handlers.
I've spent the last few hours searching Google and the articles and forums in this site, and while I've found plenty of great resources on HOW to implement custom event handlers, I haven't found any that explain WHY I would want to.
For example, one article (in Visual Studio's Help) gave an example of an AlarmClock. The AlarmClock would raise an Alarm event and a listener for that event would execute some code. Why is this model better than simply calling a Ring() method of the AlarmClock class?
Why would this:
protected virtual void OnAlarm(AlarmEventArgs e)<br />
{<br />
AlarmEventHandler handler = Alarm; <br />
if (handler != null) <br />
{ <br />
handler(this, e); <br />
}<br />
}
be better than this:
protected void Ring()<br />
{<br />
}
Any answers (including links to articles) would be very appreciated.
Thanks,
Jorsh
|
|
|
|
|
|
Hello,
First of all let me say I never worked with RSS as a developer.
I am working on a web site where I am displaying news from a database.
This part is done.
Basically, I need to make those same news available as through RSS.
I believe I need to have a XML file available and updated from the
database?
And maybe the database fields should be the same as RSS 2.0 format?
Anyway, could someone advice me on this?
Thanks,
Miguel
|
|
|
|
|
|
Hi,
I need to retrieve a single record from an SQL database and then store each field of the record as a set of variables, in order that i can then display this data on the page via the codebeind.
Im new to .Net 2.0 and am using Visual Studio 2005. I've already created a stored proceedure in SQL database and created an SQLDataSource to retrieve the information. However my problem is that i dont know how to then get the record fields from the SQLDataSource to save them as variables.
Please help.
|
|
|
|
|
I know a long winded way in which you can do this ..
Created a number of distint and simple DataGrids, each which displays ONLY 1 field from that table. They you can toggle each datagrid's visibility on and off.
This method can also be varied in a number of ways using DataLists and what not.
Hope this helps.
|
|
|
|
|
Hi,
Thanks for the answer but this seems a bit too cumbersome and inefficient for the server.
Surely there is a better way to do it? Even if it means hand coding the data retrieval from the Database.
Any ideas?
|
|
|
|
|
By posting this, I have put your hopes up, and for that, I apologise.
Just wanna say that I have no ideas, hope someone has. I would benefit from it too.
Happy Coding..
|
|
|
|
|
Thanks for the idea though. I just need a more robust way to do it.
Anyone got any ideas on how to help?
|
|
|
|
|
|
Ok thanks.
I thought as much. With the advances made over .NEt 1.1 i thought 2.0 might have also dealt with issues like this.
Obviously not, but still it seems 2.0 is a vast improvement in having closed the gap between the ease of using wizards and the difficulty of being able to do stuff a little more complex than eating a banana.
|
|
|
|
|
Hi there,
Just want to add some information to the above two guys, basically there are a couple of ways that you can use to display data in the web page. You can write your code to pull data out of DB then display it in the web page by either setting the UI control's property or bind it to the data-bound control, and at least you need to write two lines of code in the case of data binding:
Line 1: control.DataSource = datasource;
Line 2: control.DataBind;
However, the new version of the ASP.NET, you no longer need to write this code any more. And the new set of data source controls such as SqlDataSource , ObjectDataSource controls ... will handle that for you. Though you can use the datasource control in code to pull data for example calling the Select method the accessing the IEnumerable result, you'd normally use it with the data-bound controls that have the DataSourceID property like GridView, FormView .... That being said, in the case of the controls that do not have the DataSourceID property like TextBox ... you can use your code (or the datasource control) to pull data, then set to the right property of the control in code or use the data binding expression (and remember calling the DataBind method), another option is to choose a container for it like FormView control so that you can configure using a datasource control like SqlDataSource.
|
|
|
|
|
Hi,
I just want to retrieve the info from the SQLDataAdapter so i can store it in variables, and then put these variables into a text string displayed in a textbox.
I tried using a formView etc but tand am able to display the data in the textboxes. However i have to buttons associated with these textboxes. One clears all data from the textboxes whilst the other submits the information in the textboxes (form).
My problem here is that when i click either button an error is thrown up saying:
<code>Object reference not set to an instance of an object.</code> In relation to any TextBox i try working with. Even though i use <code>((TextBox)FindControl("TextBox1")).Text = ""</code> to find the TextBox and then empty it (in the case of the reset action / button clicked).
Any ideas why this is or how to overcome it?
THANKS
|
|
|
|
|
munklefish wrote: Object reference not set to an instance of an object.
Do you mean you get this error at the server side?
Because, the textbox is placed inside a FormView which is a naming container, so make sure that you call FormView1.FindControl("TextBox1") (assuming the FormView1 is the id of the FormView control). If you simply call the FindControl("TextBox1"), it's assumed that you call the method of the Page instance, and it only searchs for the textbox in the scope of the Page, not the FormView. For more information, see Accessing ASP.NET Controls Programmatically [^]
|
|
|
|
|
Hi,
I tried doing this in FormView and Repeater etc etc. However it still comes up with the same error. I have also tried to FindControl using the appropriate display method as the prefix eg Repeater1.FindControl("TextBox1") but it still says the object reference is invalid.
Could this be anything to do with the fact that im using the SQLDataSource wizard and binding it on 'Page_Load' ? Ie so effectively when i click the reset or submit button in my form, the page is reloaded and thus the referenced TextBox isnt actually available for this minute period of time during which the SQLDataSource reloads the Repeater / FormView data?
Is there anyway to add the equivalent of if(!Page.IsPostBack) to stop the Data rebinding on Postback / when a button is clicked?
Thanks.
|
|
|
|
|
I finally found the perfect solution to this problem.
You can simply capture the SqlDataSource created by the wizard, and then convert it into a new DataReader instance in the code behind file.
SqlDataReader reader = (SqlDataReader)<br />
SqlDataSource1.Select(DataSourceSelectArguments.Empty);
You can then manipulate the data in just the same way as you would a conventional / hand coded DataReader.
Hope this might help others in future. Thanks for everyone who gave advice.
|
|
|
|
|
Hi,
I have a column in my DataGrid which represents a foreign key. At the moment, it displays the ID for that key. I want it to display another column instead..
For example:
User Table has (ID, Title, Firstname, Lastname)
Title Table has (ID, Mr, Mrs)
I want the data grid to show "title" as "Mr" or "Mrs" instead of "01" or "02",
Please help, Thanks!
|
|
|
|
|
Try this......
go to Datagrid property builder.. select the Bound column for ID and set its Text Field as Mr or Ms but DataField as ID
should work
Sanjeev
|
|
|
|
|
Sorry I should have been clearer...
The Title Table is expandable. So "Ms" and "Miss" and say "Master" can be added at a later date. This means the Column will be able to display the "title" dynamicly by querying the Title Table.
Otherwise, the senario would be that I would just be able to input "Mr" and "Mrs" as regular strings.
My issue is how exactly to query the table. I know I have to use an SQL View, but my efforts have been in vain
|
|
|
|
|
put your code in The ItemDataBound Event of the Datagrid.
Thanks & regards,
Suresh Dayma
Everything Is Possible!
|
|
|
|
|
Here is the code from ASP.NET 1.1 that works. My problem is below with 2.0 GridView?
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int playerID = Convert.ToInt32(e.Item.Cells[1].Text);
string sqlDelete = "delete Players where PlayerID = '" + playerID +"'";
SqlConnection conn = new SqlConnection(dsn);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlDelete;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
DataGrid1.EditItemIndex = -1;
BindGrid();
}
This above code is ASP.NET 1.1 code for Deletion from DataGrid. It works fine. Now, I am using GridView (ASP.NET 2.0) The problem I am having is getting the playerID from GridView?
***int playerID = Convert.ToInt32(e.Item.Cells[1].Text); **
does not give any playerID but throws error? How do I get the playerID from GridView. Is the event handler Row Deleting or Row Deleted?
Please help.............
-- modified at 16:34 Wednesday 27th September, 2006
Sanju
|
|
|
|
|
could you please post the error message you're getting?
|
|
|
|
|
Thanks for you immediate reply........
Here is the error !!!
'System.Web.UI.WebControls.GridViewDeleteEventArgs' does not contain a definition for 'Item'
How do I retrieve PlayerID from the GridView?
Thanks
Sanjeev
|
|
|
|
|
San, that is weird, it is the sam way I do it (in VB.net) under the DataGrid Delete or Edit item methods (ASP.NET 1.1) , honestly I never worked with an ASP.NET 2.0 GridView object before.
Sorry!
|
|
|
|