GridView更新和刪除顯示提示資訊

來源:互聯網
上載者:User

以前做Asp.Net 1.X是經常會碰到處理清單控制項的提示情況,現在開始學習Asp.Net2.0了, 用GridView完成相似的事情,更新和刪除顯示提示資訊,確認後使用SqlDataSource
完成資料庫操作的 

<%@ Page Language="C#" %>

<script runat="server">

    void GridView1_RowCommand(object sender, GridViewCommandEventArgs
e)
    {
        switch (e.CommandName.ToLower())
       
{
            case "edit":
                GridView1.Columns[0].Visible =
false;
                GridView1.Columns[1].Visible =
true;
                break;
            case "update":
           
case "delete":
            case "cancel":
               
GridView1.Columns[0].Visible = true;
               
GridView1.Columns[1].Visible = false;
                break;
           
default:
                // Do nothing.
                break;
       
}
    }

    void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs
e)
    {
        int index = GridView1.EditIndex;
        GridViewRow
row = GridView1.Rows[index];

        TextBox firstNameTextBox =
(TextBox)row.Cells[1].FindControl("FirstNameTextBox");

        String firstName = "";
        if (firstNameTextBox !=
null)
        {
            firstName = firstNameTextBox.Text;
       
}

        TextBox lastNameTextBox =
(TextBox)row.Cells[2].FindControl("LastNameTextBox");

        String lastName = "";
        if (lastNameTextBox !=
null)
        {
            lastName = lastNameTextBox.Text;
       
}

        Parameter lastNameParameter = new Parameter("au_lname",
TypeCode.String, lastName);
        Parameter firstNameParameter = new
Parameter("au_fname", TypeCode.String, firstName);

        SqlDataSource1.UpdateParameters.Clear();
       
SqlDataSource1.UpdateParameters.Add(lastNameParameter);
       
SqlDataSource1.UpdateParameters.Add(firstNameParameter);
    }

    void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs
e)
    {
        SqlDataSource1.DeleteParameters.Clear();
    }
   

</script>

<html xmlns="http://www.w3.org/1999/xhtml"
>
<head runat="server">
    <title>GridView更新和刪除顯示提示資訊-
王智-2006</title>
</head>
<body>
    <form
id="form1" runat="server">
    <div>
        <asp:GridView
ID="GridView1" runat="server"
            DataSourceID="SqlDataSource1"
AutoGenerateColumns="False" DataKeyNames="au_id"
OnRowCommand="GridView1_RowCommand" OnRowUpdating="GridView1_RowUpdating"
OnRowDeleting="GridView1_RowDeleting">
           
<Columns>
               
<asp:TemplateField>
                   
<ItemTemplate>
                        <asp:LinkButton ID="Edit"
runat="server"
CommandName="Edit">編輯</asp:LinkButton>
                   
</ItemTemplate>
               
</asp:TemplateField>
                <asp:TemplateField
Visible="False">
                   
<ItemTemplate>
                        <asp:LinkButton ID="Update"
runat="server" CommandName="Update" OnClientClick="return
confirm('確定要更新?');">更新</asp:LinkButton>&nbsp;&nbsp;
                       
<asp:LinkButton ID="Delete" runat="server" CommandName="Delete"
OnClientClick="return
confirm('確定要刪除');">刪除</asp:LinkButton>&nbsp;&nbsp;
                       
<asp:LinkButton ID="Cancel" runat="server"
CommandName="Cancel">取消</asp:LinkButton>
                   
</ItemTemplate>
               
</asp:TemplateField>
                <asp:templatefield
headertext="Last Name">
                   
<itemtemplate>
                        <%#Eval("au_lname")
%>
                    </itemtemplate>
                   
<edititemtemplate>
                        <asp:textbox
id="LastNameTextBox" text='<%#Eval("au_lname")
%>'
                            width="175"
runat="server"/>
                       
<br/>
                        <asp:requiredfieldvalidator
id="LastNameRequiredValidator" controltovalidate="LastNameTextBox"

                            errormessage="Please enter a last name."
validationgroup="NameGroup" runat="server"/>
                   
</edititemtemplate>
               
</asp:templatefield>
               
               
<asp:templatefield headertext="First Name">
                   
<itemtemplate>
                        <%#Eval("au_fname")
%>
                    </itemtemplate>
                   
<edititemtemplate>
                          <asp:textbox
id="FirstNameTextBox" text='<%#Eval("au_fname")
%>'
                            width="175"
runat="server"/>
                         
<br/>
                          <asp:requiredfieldvalidator
id="FirstNameRequiredValidator"
controltovalidate="FirstNameTextBox"
                               
errormessage="Please enter a first name." validationgroup="NameGroup"
runat="server"/>
                    
</edititemtemplate>
               
</asp:templatefield>
            </Columns>
       
</asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1"
runat="server" ConnectionString="<%$ ConnectionStrings:Pubs2000
%>"
            ProviderName="<%$
ConnectionStrings:Pubs2000.ProviderName %>" SelectCommand="SELECT au_id,
au_lname, au_fname FROM authors"
            UpdateCommand="UPDATE authors
SET au_lname = @au_lname, au_fname = @au_fname WHERE (au_id = @au_id)"

            DeleteCommand="DELETE FROM authors WHERE (au_id =
@au_id)">
        </asp:SqlDataSource>
    </div>
   
</form>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.