asp.net 2.0 insert new record with GridView

Source: Internet
Author: User
Tags eval insert
Asp.net| inserted to see a foreign giant cow blog, probably speaking asp.net 2.0 with the GridView Insert new records, the method is more special, but the effect
Generally, so the program to turn, simpler, do not explain and so on.
<%@ Page language= "C #" Classname= "default_aspx"%>



<! DOCTYPE HTML PUBLIC "-//w3c//dtd XHTML 1.1//en" "Http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" >



<script runat= "Server" >
void Cancelbutton1_click (object sender, EventArgs e)
{

Gridview1.showfooter = false;

}
void Addbutton1_click (object sender, EventArgs e)
{

Gridview1.showfooter = true;

}



void Button1_Click (object sender, EventArgs e)

{

TextBox CustomerID = GridView1.FooterRow.FindControl ("Customeridtextbox") as TextBox;

TextBox companyName = GridView1.FooterRow.FindControl ("Companynametextbox") as TextBox;

DropDownList ContactTitle = GridView1.FooterRow.FindControl ("Contacttitledropdownlist") as DropDownList;



sqldatasource1.insertparameters["CustomerID"]. DefaultValue = Customerid.text;

sqldatasource1.insertparameters["CompanyName"]. DefaultValue = Companyname.text;

sqldatasource1.insertparameters["ContactTitle"]. DefaultValue = Contacttitle.selectedvalue;



Sqldatasource1.insert ();

}



</script>





<title>untitled page</title>


<body>

<form id= "Form1" runat= "Server" >

<div>
<asp:button id= "AddButton1" runat= "Server" text= "Add new Item" onclick= "Addbutton1_click"/>


<asp:gridview id= "GridView1" runat= "Server" datasourceid= "SqlDataSource1" datakeynames= "CustomerID"

autogeneratecolumns= "False" showfooter= "True" >

<Columns>

<asp:TemplateField>

<ItemTemplate>

<asp:label id= "Customeridlabel" runat= "Server" ><%# Eval ("CustomerID")%></asp:label>

</ItemTemplate>

<FooterTemplate>

<asp:textbox id= "Customeridtextbox" runat= "Server" ></asp:TextBox>

</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField>

<ItemTemplate>

<asp:label id= "Companynamelabel" runat= "Server" ><%# Eval ("CompanyName")%></asp:label>

</ItemTemplate>

<FooterTemplate>

<asp:textbox id= "Companynametextbox" runat= "Server" ></asp:TextBox>

</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField>

<FooterTemplate>

<asp:dropdownlist id= "contacttitledropdownlist" runat= "datasourceid=" SqlDataSource2 "datatextfield=" ContactTitle "datavaluefield=" ContactTitle ">

</asp:DropDownList>

<asp:sqldatasource id= "SqlDataSource2" runat= "Server" selectcommand= "SELECT DISTINCT [ContactTitle] from [ Customers] "

connectionstring= "Server=localhost;uid=sa;password=xxx;database=northwind" >

</asp:SqlDataSource>

<asp:button id= "Button1" runat= "Server" text= "Add" onclick= "Button1_Click"/>
<asp:button id= "CancelButton1" runat= "Server" text= "Cancel" onclick= "Cancelbutton1_click"/>



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.