asp.net DetailsView update and use method

Source: Internet
Author: User

DetailsView updates and the operation of the GridView DetailsView update protected void Detailsview1_itemupdating (object sender,

DetailsViewUpdateEventArgs e)
{
string tableid = URL;
string tablename = Tbname;
String M_monthcount;
String M_messagecount;
String M_remark;
String sqlupdate; TextBox TB;
TB = (textbox) (Detailsview1.rows[1].findcontrol ("TextBox2"));
M_monthcount = Tb.text; TB = (textbox) (Detailsview1.rows[2].findcontrol ("TextBox3"));
M_messagecount = Tb.text; TB = (textbox) (Detailsview1.rows[3].findcontrol ("textbox4"));
M_remark = Tb.text; sqlupdate = "Update" + tablename + "Set M_monthcount = '" + M_monthcount + "', M_messagecount = '" + M_messagecount + ",        M_remark = ' "+ M_remark +" ' where m_id = ' "+ TableID +" ' "; SqlConnection con = Db.createsqlcon ();
Con.open ();
SqlCommand cmd = new SqlCommand (Sqlupdate,con);
Cmd.executenonquery ();
Con.close ();////////////////////////////////////////set DetailsView default mode to edit mode//////////////////////////////////// <asp Tutorial: DetailsView id= "Detailsview1" runat= "Server" height= "55px" width= "370px" autogeneraterows = "false" cellpadding= "4" forecolor= "#333333" gridlines= "None" onitemupdating= "detailsview1_itemupdating" Defaultmode= "Edit" >
<fields>
<asp:templatefield headertext= "Company Name" > <edititemtemplate>
<asp:textbox id= "TextBox1" runat= "Server" width= "200px" readonly= "true" text= ' <%# bind ("M_name")%> ' >< /asp:textbox>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext= "Total number of articles" >
<edititemtemplate>
<asp:textbox id= "TextBox2" runat= "Server" width= "200px" text= ' <%# bind ("M_monthcount")%> ' ></asp: Textbox>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext= "Current Send" >
<edititemtemplate>
<asp:textbox id= "TextBox3" width= "200px" runat= "server" text= ' <%# bind ("M_messagecount")%> ' ></asp: Textbox>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext= "Remarks" > <edititemtemplate>
<asp:textbox id= "textbox4" runat= "Server" height= "121px" text= ' <%# bind ("M_remark")%> '
Textmode= "Multiline" width= "200px" ></asp:textbox>
</edititemtemplate>
</asp:templatefield>
<asp:buttonfield buttontype= "button" commandname= "Update" text= "buttons" >
</asp:buttonfield>
</fields>
<footerstyle backcolor= "#507cd1" font-bold= "true" forecolor= "white"/>
<commandrowstyle backcolor= "#d1ddf1" font-bold= "true"/>
<editrowstyle backcolor= "#2461bf"/>
<rowstyle backcolor= "#eff3fb"/>
<pagerstyle backcolor= "#2461bf" forecolor= "White" horizontalalign= "center"/>
<fieldheaderstyle backcolor= "#dee8f5" font-bold= "true"/>
<alternatingrowstyle backcolor= "White"/>
</asp:detailsview>////////////////////////////////////////////////gridview Operation/////////////////////////// Cancel edit ***************/protected void gridview1_ Rowcancelingedit (object sender, Gridviewcancelediteventargs e)
{
Gridview1.editindex =-1;
Datagridviewbind1 ();
}/******************* Editor ***********************/
protected void Gridview1_rowediting (object sender, GridViewEditEventArgs e)
{
Gridview1.editindex = E.neweditindex;
Datagridviewbind1 ();
}/********************* Update Operation ******************************/
protected void Gridview1_rowupdating (object sender, Gridviewupdateeventargs e)
{
String updatacmd = "Update" + Sqldatatime + "set m_monthcount= '"
+ ((textbox) (Gridview1.rows[e.rowindex].cells[1].controls[0])). Text.tostring (). Trim () + "', m_messagecount= '"
+ ((textbox) (Gridview1.rows[e.rowindex].cells[2].controls[0])). Text.tostring (). Trim () + "', m_remark= '"
+ ((textbox) (Gridview1.rows[e.rowindex].cells[3].controls[0])). Text.tostring (). Trim () + "' Where m_id= '"
+ gridview1.datakeys[e.rowindex].value.tostring () + "'";
SqlConnection con = Db.createsqlcon ();
SqlCommand cmd = new SqlCommand (Updatacmd, con); Con.open ();
Cmd.executenonquery ();
Gridview1.editindex =-1;
Datagridviewbind1 ();
Con.close ();
}


Here's a look at how to use DetailsView in the ASP.net tutorial

<asp:detailsview id= "Detailsview1" runat= "Server" autogeneraterows= "false" height= "50px"
Width= "500px" onmodechanging= "detailsview1_modechanging" onitemdeleting= "detailsview1_itemdeleting"
onitemupdating= "detailsview1_itemupdating" oniteminserting= "detailsview1_iteminserting" >
<fields>
<asp:templatefield headertext= "id" >
<itemtemplate>
<% #eval ("id")%>
</itemtemplate>
<insertitemtemplate>
<asp:textbox id= "Txtinsertid" text= "Insertid" runat= "Server"/>
</insertitemtemplate>
<edititemtemplate>
<% #eval ("id")%>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext= "title" >
<itemtemplate>
<% #eval ("title")%>
</itemtemplate>
<insertitemtemplate>
<asp:textbox id= "Txtinserttitle" text= "Inserttitle" runat= "Server"/>
</insertitemtemplate>
<edititemtemplate>
<asp:textbox id= "Txtedittitle" text= ' <%# eval ("title")%> ' runat= ' server '/>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext= "Context" >
<itemtemplate>
<%# eval ("Logcontext")%>
</itemtemplate>
<insertitemtemplate>
<asp:textbox id= "Txtinsertlogcontext" text= "Insertlogcontext" runat= "Server"/>
</insertitemtemplate>
<edititemtemplate>
<asp:textbox id= "Txteditlogcontext" text= ' <%# eval ("Logcontext")%> ' runat= ' server '/>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext= "Operations" >
<itemtemplate>
<asp:button id= "Btnedit" runat= "Server" causesvalidation= "false"
Commandname= "Edit" text= "editor"/>
<asp:button id= "btnnew" runat= "Server" causesvalidation= "false"
Commandname= "new" text= "newly created"/>
<asp:button id= "Btndelete" runat= "Server" causesvalidation= "false"
Commandname= "Delete" text= "Remove" onclientclick= "return confirm (' OK to update this student information? '); "/>
</itemtemplate>
<insertitemtemplate>
<asp:button id= "Btninsert" runat= "Server" causesvalidation= "true"
Commandname= "Insert" text= "inserts"/>
<asp:button id= "Btncancel" runat= "Server" causesvalidation= "false"
Commandname= ' Cancel ' text= ' Cancel '/>
</insertitemtemplate>
<edititemtemplate>
<asp:button id= "btnupdate" runat= "Server" causesvalidation= "true"
Commandname= "Update" text= "onclientclick=" return confirm (' OK to update this student information? '); "/>
<asp:button id= "Btncancel2" runat= "Server" causesvalidation= "false"
Commandname= ' Cancel ' text= ' Cancel '/>
</edititemtemplate>
</asp:templatefield>
</fields>
</asp:detailsview>

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.