. Net website development knowledge point 2

Source: Internet
Author: User

Net website development knowledge point 1
This section introduces Form verification, the use of FCKediter controls, and the use of paging controls. This chapter introduces some simple knowledge points and master these knowledge to make a simple news publishing website.
I. News Headlines

You can use the reapeter control to preview news titles.
 
[Html]
<Asp: Repeater ID = "Repeater1" runat = "server">
<ItemTemplate>
<Table width = "550" border = "1" cellpadding = "0" style = "border: none">
<Tr>
<Td height = "30px" style = "border-left-width: 0; border-top-width: 0; border-right-width: 0; border-bottom-width: 0 "class =" text "> <% # Convert. toDateTime (Eval ("NewsTime ")). tow.datestring () %> </td>
</Tr>
<Tr>
<Td style = "border-left-width: 0; border-top-width: 0; border-right-width: 0; border-bottom-width: 0">
<Div style = "text-align: justify; font-size: 12pt; color: #306; font-family: Arial, Helvetica, sans-serif; font-style: italic; font-weight: bold; width: 550px; overflow: hidden "> <% # Eval (" NewsTitle ") %> </td>
</Tr>

<Tr>
<Td style = "border-left-width: 0; border-top-width: 0; border-right-width: 0; border-bottom-width: 0; padding-top: 10px "class =" text ">
<Div style = "text-align: justify; width: 550px; overflow: hidden"> <% # getchar (Eval ("NewsText "). toString (), 340) %> </td>
</Tr>
 
<Tr>
<Td height = "30px" style = "border-left-width: 0; border-top-width: 0; border-right-width: 0; border-bottom-color: #666; "> <br/> <a href =" NewsView. aspx? Id = <% # Eval ("id") %> "class =" more "> More </a> </td>
</Tr>
</Table>
</ItemTemplate>
</Asp: Repeater>
The background code is as follows:
[Csharp]
Namespace WebAPP
{
Public partial class NewsReleases: System. Web. UI. Page
{
Private NewsManage newsManage = new NewsManage ();
 
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
Repeater1.DataSource = newsManage. GetList ();
Repeater1.DataBind ();
}
}
 
Public string getchar (string str, int len)
{
Str = Regex. Replace (str, @ "<[^>] +> ","");
If (str. Length> len)
Str = str. Substring (0, len );
Str + = "... ";
Return str;
}
}
}
There is also a title type:


In this case, you can use datalist or repeater to bind:
[Html]
<Div class = "productList">
<Asp: Repeater ID = "Repeater1" runat = "server">
<HeaderTemplate>
<Ul>
</HeaderTemplate>
<ItemTemplate>
<Li>
<Table border = "0">
<Tr> <td valign = "middle"> "width =" 65px "height =" 75px "style =" margin-right: 5px; border: 1px solid black; "/> </td>
<Td valign = "top" height = "85px" width = "125px" style = "font-size: 12px; word-spacing: 3px; text-align: left "> <a href =" Media <% # Eval ("fileType") %>. aspx? Id = <% # Eval ("id") %> "target =" _ blank "class =" bottom "> <% # Eval (" fileTitle ") %> </a>
<Div style = "width: 125px"> <% # Eval ("fileMonth") %> <% # Eval ("fileYear") %> </div>
</Td> </tr>
</Table>
</Li>
</ItemTemplate>
<FooterTemplate>
</Ul>
</FooterTemplate>
</Asp: Repeater>
</Div>

You need to set a certain CSS style for the Reperter:
[Html]
. ProductList {
Border: none;
Margin: 0px;
Width: 630px;
}

. ProductList ul {
Margin: 0px;
Padding: 0px;
List-style: none;

}

. ProductList li {
Float: left;
Padding-right: 10px;
Width: 200px;
List-style: none;
}

Ii. Background editing Module

As described in the previous chapter, it is also very easy to bind data to the Gridview. Here we will talk about how to operate this template item.
The Code is as follows:
[Html]
<Asp: TemplateField HeaderText = "operation">
<ItemTemplate>
<Asp: Button ID = "btnView" runat = "server" Text = "View Details" ForeColor = "# 0063DC" CommandName = "View" CommandArgument = "<% # Container. displayIndex %> "/>
<Asp: Button ID = "btnCancel" runat = "server" Text = "delete" ForeColor = "# 0063DC" CommandName = "Cancel" CommandArgument = "<% # Container. displayIndex %> "/>
</ItemTemplate>
<ItemStyle Width = "130px" ForeColor = "# 0063DC"/>
</Asp: TemplateField>

<Container. DisplayIndex> you can determine the row number.
Background code:
[Csharp]
Namespace WebAPP
{
Public partial class EditNews: System. Web. UI. Page
{
Private NewsManage newsManage = new NewsManage ();
 
/// <Summary>
/// The GridView binds all data.
/// </Summary>
Public void GridViewBind ()
{
TxtRowCount. Text = newsManage. ExecuteScalar (). ToString ();
AspNetPager. RecordCount = newsManage. ExecuteScalar ();
Int intStart = AspNetPager. PageSize * (AspNetPager. CurrentPageIndex-1 );
Int intNum = AspNetPager. PageSize;
DataGridView. DataSource = newsManage. GetList (intStart, intNum );
DataGridView. DataBind ();
}
 
Protected void Page_Load (object sender, EventArgs e)
{
Try
{
If (! IsPostBack)
{
GridViewBind ();
}
}
Catch (Exception ex)
{Throw ex ;}
}
 
Protected void maid (object sender, GridViewCommandEventArgs e)
{
Int rowIndex = Convert. ToInt32 (e. CommandArgument );
Int id = Convert. ToInt32 (DataGridView. Rows [rowIndex]. Cells [0]. Text );
If (e. CommandName = "Cancel ")
{
Try
{
NewsManage. Delete (id );
 
}
Catch (Exception ex)
{Throw (ex );}
}
 
If (e. CommandName = "View ")
{
Response. Redirect ("NewsDetail. aspx? Id = "+ id );
}
}
 
Protected void maid (object sender, GridViewCancelEditEventArgs e)
{
Try
{
DataGridView. EditIndex =-1;
GridViewBind ();
}
Catch (Exception ex)
{Throw ex ;}
}
 
Protected void maid (object sender, GridViewRowEventArgs e)
{
If (e. Row. RowType = DataControlRowType. DataRow)
{
(Button) e. row. cells [2]. controls [3]). attributes. add ("onclick", "javascript: return confirm ('Are you sure you want to delete this item? ')");
}
}
 
Protected void DropListNumber_TextChanged (object sender, EventArgs e)
{
Try
{
// Select the number of data lines displayed
AspNetPager. PageSize = Convert. ToInt32 (DropListNumber. SelectedValue );
GridViewBind ();
}
Catch (Exception ex)
{Throw ex ;}
}
 
Protected void AspNetPager_PageChanged (object sender, EventArgs e)
{
Try
{
GridViewBind ();
}
Catch (Exception ex)
{Throw ex ;}
}
}
}

Related Article

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.