ASP. NET: Like Web Form, or drag controls (6)

Source: Internet
Author: User

There is an article on the Internet from CSDN qingyueer called GridView72, which is widely used. The 72-year-old stunt is the skill of Shaolin, and the step-by-step approach is the main path of martial arts. However, it is not appropriate for some undiligent people, such as when I am so lazy to drag controls. Martial arts requires no skill, no skill, no code, no code, and no code for programmers. I think most of the young programmers do not have a girlfriend (strange, why should I mention this). Therefore, I want to prefer code-less in any aspect.

In this article, we will pick out a complex technical exercise: the GridView and CheckBox combine to delete multiple rows in the table. Effect

First, we should design this function from the database. To delete unrelated multiple rows at a time, we must use the stored procedure, first, write a stored procedure for deleting multiple rows of data:

 

Alter procedure dbo. DelMultiLine
(
@ Ids nvarchar (300)
)
AS
/* Set nocount on */
DECLARE @ start INT
DECLARE @ id INT
DECLARE @ idtable TABLE (id INT)
SET @ start = 0
While (@ start <DATALENGTH (@ ids ))
BEGIN
DECLARE @ end INT
SET @ end = CHARINDEX (',', @ ids, @ start)
IF @ end = 0 SET @ end = DATALENGTH (@ ids)
SET @ id = CAST (SUBSTRING (@ ids, @ start, @ end-@ start) as int)
Insert into @ idtable VALUES (@ id)
SET @ start = @ end + 1
END
Delete from [Commodity] WHERE (id IN (SELECT * FROM @ idtable ))

/* Set nocount off */
RETURN

 

Someone may ask, why don't you just get it done without exec ('delete from [table] where ID in ('+ @ ids +? The answer is security. Ids is a string submitted by the user, so exec is very dangerous. Always remember All inputs are edevils.

With the stored procedure, everything is fine. Next we need to write a FormView to submit it to the stored procedure, and let the FormView get the Id of all the selected rows in the GridView. The FormView creation process is the same as before, but we select the Stored Procedure for the Data source:

In fact, it doesn't matter which statement to select. Here I have bound the delete multi-row stored procedure in the Insert statement. We only need to select and edit the corresponding template in FormView. Then, a window will pop up asking you to select the parameter source. Our parameter should come from the GridView but it does not have the "selected row ID" attribute, so we will do some work later, now we can decide to put the information of this parameter into a Cookie or a Hidden Field. Here we see the Hidden Field.

Unfortunately, VS2008 has a bug. It can only identify the parameters of the Select statement. Therefore, we need to manually edit the ASP. NET code and move the parameters of the Select statement to Insert:

 

 

<Asp: SqlDataSource ID = "DeleteCommodityDataSource" runat = "server"
ConnectionString = "<% $ ConnectionStrings: ConnectionString %>"
InsertCommand = "DelMultiLine" InsertCommandType = "StoredProcedure"
SelectCommand = "DelMultiLine" SelectCommandType = "StoredProcedure">
<SelectParameters>
</SelectParameters>
<InsertParameters>
<Asp: ControlParameter ControlID = "SelectedIDs" Name = "ids" PropertyName = "Value"
Type = "String"/>
</InsertParameters>
</Asp: SqlDataSource>

 

So far, we only need to get the selected information of the GridView to the Hidden Field. But first, we need to add the columns containing the Check Box to the Grid. This should be a TemplateColumn. Edit the TemplateColumn template and add the CheckBox to it.

How can we associate CheckBox with Hidden Field? The answer is JavaScript. We need to bind the product ID to the CheckBox code, and then send it to JavaScript as a parameter for processing:

<Asp: TemplateField HeaderText = "Select">
<ItemTemplate>
<Asp: CheckBox ID = "CheckBox1" OnClick = '<% # Eval ("id", "UpdateSelectedIds ({0}, this. checked) ") %> 'runat =" server "/>
</ItemTemplate>
</Asp: TemplateField>

 

Next, we only need to update the hidden SelectedIDs in UpdateSelectedIds. The Code is as follows:

Function UpdateSelectedIds (id, value ){
If (document. getElementById ("SelectedIDs"). value! = "")
Var ids = document. getElementById ("SelectedIDs"). value. split (",");
Else
Ids = [];
Var table = [];
For (var I = 0; I <ids. length; I ++ ){
Table [Number (ids [I])] = true;
}

Table [id] = value;
Ids = [];
For (var I = 0; I <table. length; I ++ ){
If (table [I]) ids. push (I)
}
Document. getElementById ("SelectedIDs"). value = ids. toString ();
Alert (document. getElementById ("SelectedIDs"). value );
}

 

After debugging all the functions, run the command to check the effect.

Thank you for reading this article. Merry Christmas to everyone.

I will continue to offer some tips for no code (especially for no C.

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.