The DataGrid gives the DataGrid the value of clicking the postback event and bringing back the specified field
When we encounter the master-slave table structure, we often have this kind of demand
That is to use a DataGrid to list the primary table's information, and then select a record in the main table to have another DataGrid display information from the table based on the value of the main external health. But in the DataGrid event to meet our needs, we need to add a Onselectpostback event to the DataGrid, then we each select a master table record will trigger a return, in the event we get to the specified data source of the specified field values, binding information from a table
Let's get us to expand the DataGrid and get a great development code: Selectpostbackdatagrid
Basically what we need to expand is:
1. Events
public delegate void Selectpostbackeventhandler (object sender, Selectpostbackeventargs e);
public event Selectpostbackeventhandler Onselectpostback;
Because we want to return the arguments, we want to customize our own delegate events so that we can pass the value of the specified field through the event arguments
Custom event parameters, we need the line to be cited and returned by the field value on it, of course, what you like to return anything, as long as you are happy.
set {viewstate[base. UniqueID + "Allowselectpostback"] = value; }
}
We have multiple attribute allowselectpostback to set the DataGrid that does not need to be sent back, perhaps to grasp the extension of this one when the common DataGrid is used to be! Of course, it is also good to judge whether an event has been decided to return.
The reason for saving to view state is that it is well known that I don't have to say more!
3, let the DataGrid have the click of a postback event
Seems to be a very wonderful thing let's look at the DataGrid in the page structure
On the page on the previous <TR></TR> corresponds to the line, then add click event is to <TR> add onclick event on it, or add double click event, haha! As long as you like. <TR> corresponding to the DataGrid background object is an item, then this item is a row of DataGrid, according to the DataGrid initialization item order, first is the head, then the body, then the horn. Ha ha! It's from top to bottom. So there are several types of item, we just need to add a click event to the data row, nothing else is required, that is, the item's itemtype is Item,alternatingitem,selecteditem row Add Click event is OK
Of course we do what we do when we initialize the line, so in the constructor we add the event of the initialization line, and then we do the above work in our event function, and notice that we're going to fetch the value of the field we specify to return in each row.
So how do we accept the parameters that come back from the page? One of the most important things to note here is that you want to get back the parameters, the smartest way is to implement the IPostBackEventHandler interface, of course, you can also like to use other methods, you may need to send back a number of complex data, as long as you are smart enough, any data can be worn, The power of the web is strong, Nero is great, we are great, because we have IPostBackEventHandler interface, like a space-time tunnel, do not need anything superfluous. haha More and more far away.)
Implementation of this interface
public virtual void RaisePostBackEvent (String eventargument)
{
int index;
string fieldvalue = null;
if (Selectpostbackdatafield!= null)
{
index = Int. Parse (eventargument.substring (0, Eventargument.indexof ('_')));
int beginindex = Eventargument.indexof ('_') + 1;
int leng = Eventargument.length-eventargument.indexof ('_')-1;
Onselectpostback (This, new Selectpostbackeventargs (Index, fieldvalue));
}
}
The thing to do is to process the parameters of the page back, instantiate our custom event parameters, and then trigger our event, and it's over. Of course, the event that is added with this component is also raised, and the value in our custom parameter is taken.
The simple thing is to achieve the click of the return event, of course you can do a lot of things you like to do, here is just an example
Here is the complete code, as long as the copy can be compiled on the Oh!
If you like, Hope is the GPL supporter, dedicated to the technology
Using System;
Using System.Web.UI.WebControls;
Using System.Web.UI;
Using System.ComponentModel;
Namespace HL
{
public class Selectpostbackdatagrid:datagrid, IPostBackEventHandler
{
Public Selectpostbackdatagrid ()
{
Base. ItemDataBound + = new Datagriditemeventhandler (selectpostbackdatagrid_itemdatabound);
}
public delegate void Selectpostbackeventhandler (object sender, Selectpostbackeventargs e);
public event Selectpostbackeventhandler Onselectpostback;
public virtual void RaisePostBackEvent (String eventargument)
{
int index;
string fieldvalue = null;
if (Selectpostbackdatafield!= null)
{
index = Int. Parse (eventargument.substring (0, Eventargument.indexof ('_')));
int beginindex = Eventargument.indexof ('_') + 1;
int leng = Eventargument.length-eventargument.indexof ('_')-1;
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.