Let DataGrid have values ​​that click the return event and bring back the specified field.

xiaoxiao2021-03-06  58

Let DataGrid have values ​​that click the return event and bring back the specified field.

Let DataGrid have values ​​that click the return event and bring back the specified field.

When we encounter the main slave structure, we often have such a need.

That is to list the information of the primary table with a DataGrid, then select a record in the main table to make another DataGrid to display the information from the table according to the host's value. But in the DataGrid event, we need to add an onselectpostback event to DataGrid, then we will trigger a backhable every one of the primary table records, and we take the specified field of the specified data source in the event. Value, bind information from the table

Let us have to expand the DataGrid to take a very cattle of a cow: SelectPostBackDataGrid

In general, we need to extend the content:

1, event

Public Delegate Void SelectPostBackeventHandler (Object Sender, SelectPostBackeventargs E);

Public Event SelectPostBackeventhandler OnselectrPostback;

Because we want to pass the parameters, you have to customize your own commission event so that the value of our specified field is transmitted through the event parameters.

Custom event parameters, we need a field value and return field value, of course, what you like, as long as you are willing.

Public Class SelectPostBackeventargs: Eventargs

{

PRIVATE STRING N_VALUE;

PRIVATE 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 {returnirex;}

}

}

2. Data source field 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 have multiple properties allowSelectPostback to set up the need to return, perhaps the extension of this extension DataGrid is used as ordinary DataGrid to use it! Of course, it can also be judged that there is no event to decide to return to return. The reason is to save to the status of the view, this is what I know, I don't have to say more here!

3, let DataGrid owns the return event

It seems to be a very wonderful thing, let's take a look at DataGrid in the page structure

In the page, a is in line, then adding a click event is to add an onclick event to , or add a double-click event, haha! as long as you like it. Corresponding to the object of the DataGrid is an item, then this item is a line of DataGrid, according to the DataGrid initializes the order of Item, first is the head, then the body, then the angle. Haha! It is from top to bottom. So Item also has several types, we only need to add clicking events to the data line, don't need to be tubed, that is, add the item ITEMTYPE to Item, AlternatingItem, SelectedItem's row to add clicking events.

Of course, we are doing the above when we initialize the row, so we add the event of the initialized row in the constructor, then do our event functions, pay attention, we have to take each line we specified To return to the value of the field

DataBinder.eval (E.Item.DataItem, SelectPostBackDatafield) .tostring ()

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

Then we can pass this from the return parameters of the event.

What happened in the constructor and the work in the event function are as follows:

Public selectPostBackDataGrid ()

{

Base.itemdatabaseMDatabaseMeventHandler (SelectPostBackDataGrid_ItemDatabase;

}

Private void SelectPostBackDataGrid_ItemDatabase (Object Sender, DataGriditeMeventArgs E)

{

IF (! allowselectrpostback)

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 still be released! Haha!

So how do we accept from the parameters sent back from the page? One thing to pay serious attention is to take the parameters that you want to get back. The most clever method is to implement the iPostBackeventHandler interface. Of course, you can also use other ways, you may need to pass multiple complex data, as long as you are smart enough What data can wear, the power of the network is powerful, Nero is great, we are also very great, because we have an ipostbackeventhandler interface, just like a time and space tunnel, do not need any extra things. (Haha! More and more far more) realize 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 (OnsectPostback! = null)

{

OnselectPostback (this, New SelectPostBackeventargs (INDEX, FIELDVALUE);

}

}

What you do this is to process the parameters sent back, instantize our custom event parameters, then trigger our events, and things are over. Of course, the events that use this component are added, and they will be taken to our custom parameters.

Simple is to achieve incidents that click back the post, of course, you can do a lot of things you like, here is just an example.

Here is a complete code, just copy it to compile!

If you like, hope is a supporter of GPL, dedication 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.itemdatabaseMDatabaseMeventHandler (SelectPostBackDataGrid_ItemDatabase;

}

Public Delegate Void SelectPostBackeventHandler (Object Sender, SelectPostBackeventargs E);

Public Event SelectPostBackeventhandler OnselectrPostback;

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 (OnsectPostback! = 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_ItemDatabase (Object Sender, DataGriditeMeventArgs E)

{

IF (! allowselectrpostback)

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

Internal SelectPostBackeventargs (int index, string value)

{

THIS.Index = Index;

THIS.N_VALUE = Value;

}

Public String Value

{

Get {return n_value;}

}

Public Int index PUBLIC INDEX

{

Get {returnirex;}

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-112969.html

New Post(0)