Allows the DataGrid to have a click return event and bring back the value of the specified field

Source: Internet
Author: User
Allows the DataGrid to have a click return event and bring back the value of the specified field

Allows the DataGrid to have a click return event and bring back the value of the specified field

When encountering a master-slave table structure, we often have this requirement.

That is to use a DataGrid to list the information of the master table, and then select a record in the master table to display the information of the slave table based on the value of the master and Foreign keys. However, the event in the DataGrid does not meet our needs. We need to add an onselectpostback event to the DataGrid, so each time we select a primary table record, a return will be triggered, in the event, we get the value of the specified field of the specified data source and bind the information of the slave table.

Let's give us an extended DataGrid with a very good development code: selectpostbackdatagrid

In general, we need to expand the following content:

1. Events

Public Delegate void selectpostbackeventhandler (Object sender, selectpostbackeventargs E );

Public event selectpostbackeventhandler onselectpostback;

Because we want to return parameters, we need to customize our own delegate events so that the values of the specified fields can be returned through the event parameters.

For custom event parameters, we need the field values introduced and returned by the row. Of course, if you like it, you can send it back.

Public class selectpostbackeventargs: eventargs

{

Private string n_value;

Private int index;

Internal selectpostbackeventargs (INT index, string value)

{

This. Index = index;

This. n_value = value;

}

Public String Value

{

Get {return n_value ;}

}

Public int Index

{

Get {Return Index ;}

}

}

2. Data Source fields to be returned

Public String selectpostbackdatafield

{

Get

{

If (viewstate [base. uniqueid + "selectpostbackdatafield"] = NULL)

Return NULL;

Return viewstate [base. uniqueid + "selectpostbackdatafield"]. tostring ();

}

Set {viewstate [base. uniqueid + "selectpostbackdatafield"] = value ;}

}

Public bool allowselectpostback

{

Get

{

If (viewstate [base. uniqueid + "allowselectpostback"] = NULL)

Return true;

Return bool. parse (viewstate [base. uniqueid + "allowselectpostback"]. tostring ());

}

Set {viewstate [base. uniqueid + "allowselectpostback"] = value ;}

}

We need to set multiple attributes allowselectpostback without returning them. Maybe we should take the extended DataGrid as a normal DataGrid to try it! Of course, it is good to judge whether an event is being returned or not.

The reason why I want to save it to the view status is well known. I don't need to talk about it here!

3. Let the DataGrid have a click return event

It seems to be a wonderful thing. Let's take a look at the page structure of the DataGrid.

On the page, <tr> </tr> corresponds to a row. Then, you can add an onclick event to <tr> or double-click the event. Haha! As long as you like. The <tr> corresponding to the DataGrid background object is an item, so this item is a row of the DataGrid. according to the order in which the DataGrid initializes the item, first the header, then the body, and then the angle. Haha! From top to bottom. Therefore, there are several types of items. You only need to add a click event to the data row. You do not need to worry about anything else, that is, the itemtype for the item is item, alternatingitem, you can add and click an event to the row of selecteditem.

Of course, we will do the above thing during the initialization of the row, so we add the initialization row event in the constructor, and then do the above work in our event function. Note that, we need to obtain the value of the field to be returned for each row.

Databinder. eval (E. Item. dataitem, selectpostbackdatafield). tostring ()

This method obtains the value of the specified field in the row (this method is enhanced in 2.0, very good)

Then we can pass this back in the callback parameter of the event.

What happens in the constructor and what needs to be done in the event function are as follows:

Public selectpostbackdatagrid ()

{

Base. itemdatabound + = new datagriditemeventhandler (selectpostbackdatagrid_itemdatabound );

}

Private void selectpostbackdatagrid_itemdatabound (Object sender, datagriditemeventargs E)

{

If (! Allowselectpostback)

Return;

If (E. Item. itemtype = listitemtype. Item | E. Item. itemtype = listitemtype. alternatingitem | E. Item. itemtype = listitemtype. selecteditem)

{

String argstring = E. Item. itemindex. tostring ();

If (selectpostbackdatafield! = NULL)

Argstring + = "_" + databinder. eval (E. Item. dataitem, selectpostbackdatafield). tostring ();

E. Item. Attributes. Add ("onclick", page. getpostbackeventreference (this, argstring ));

}

}

Click the event to release it! Haha!

So how can we accept the parameters passed back from the page? The most important thing to note here is that you want to obtain the passed parameters. The Smartest way is to implement the ipostbackeventhandler interface. Of course, you can also use other methods, you may need to transmit multiple complex data. As long as you are smart enough, you can wear any data. The power of the network is amazing, and Nero is great, we are also great, because we have an ipostbackeventhandler interface, just like a space-time tunnel, which does not need anything extra. (Haha! )

Implement 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;

Fieldvalue = eventargument. substring (beginindex, Leng );

}

Else

Index = int. parse (eventargument );

This. selectedindex = index;

If (onselectpostback! = NULL)

{

Onselectpostback (this, new selectpostbackeventargs (index, fieldvalue ));

}

}

What we do here is to process the parameters passed back from the page, instantiate our custom event parameters, and then trigger our events. The process is over. Of course, when this component is used, the added event is triggered, and the value in our custom parameter is obtained.

The simple thing is to implement the click return event. Of course, you can do a lot of things you like to do. Here is just an example.

Below is the completeCode, Just copy and compile it!

If you like, hope to be a GPL supporter and devote yourself to 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;

Fieldvalue = eventargument. substring (beginindex, Leng );

}

Else

Index = int. parse (eventargument );

This. selectedindex = index;

If (onselectpostback! = NULL)

{

Onselectpostback (this, new selectpostbackeventargs (index, fieldvalue ));

}

}

// Attribute

Public String selectpostbackdatafield

{

Get

{

If (viewstate [base. uniqueid + "selectpostbackdatafield"] = NULL)

Return NULL;

Return viewstate [base. uniqueid + "selectpostbackdatafield"]. tostring ();

}

Set {viewstate [base. uniqueid + "selectpostbackdatafield"] = value ;}

}

Public bool allowselectpostback

{

Get

{

If (viewstate [base. uniqueid + "allowselectpostback"] = NULL)

Return true;

Return bool. parse (viewstate [base. uniqueid + "allowselectpostback"]. tostring ());

}

Set {viewstate [base. uniqueid + "allowselectpostback"] = value ;}

}

// Private Method

Private void selectpostbackdatagrid_itemdatabound (Object sender, datagriditemeventargs E)

{

If (! Allowselectpostback)

Return;

If (E. Item. itemtype = listitemtype. Item | E. Item. itemtype = listitemtype. alternatingitem | E. Item. itemtype = listitemtype. selecteditem)

{

String argstring = E. Item. itemindex. tostring ();

If (selectpostbackdatafield! = NULL)

Argstring + = "_" + databinder. eval (E. Item. dataitem, selectpostbackdatafield). tostring ();

E. Item. Attributes. Add ("onclick", page. getpostbackeventreference (this, argstring ));

}

}

}

Public class selectpostbackeventargs: eventargs

{

Private string n_value;

Private int index;

Internal selectpostbackeventargs (INT index, string value)

{

This. Index = index;

This. n_value = value;

}

Public String Value

{

Get {return n_value ;}

}

Public int Index

{

Get {Return Index ;}

}

}

}

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.