需要使用VB.net更新ASP.net Gridview中的记录 [英] Need to update a record in ASP.net Gridview using VB.net

查看:152
本文介绍了需要使用VB.net更新ASP.net Gridview中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格视图来显示表[FieldsSlNo,StuName]

我需要在运行时编辑StuName.我为此功能选择了girdviews的"EDIT UPDATE CANCEL"列.当我单击第(2)列中的编辑"链接时,编辑将变为更新取消链接",而其他两个成为文本"框,由于我无法获取文本框的名称进行编码,因此我将所有字段都转换为模板字段,我按我的意愿命名

这是源代码

Hi, I have a grid view to display a table [Fields SlNo, StuName]

I need to Edit the StuName during Run time. I chose girdviews "EDIT UPDATE CANCEL" column for this functionality. When i click Edit link in the column(2) the edit become Update Cancel Links, and the other two becomes the Text box, Since i am unable to get the name of the text box to code, i transformed all the fields in to a template field and i named as i wish

Here is the souce code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84"

            BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2"

            DataSourceID="SqlDataSource1">
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <Columns>
                <asp:TemplateField HeaderText="SlNo" SortExpression="SlNo">
                    <EditItemTemplate>
                        <asp:TextBox ID="TxtSlNo" runat="server" Text='<%# Bind("SlNo") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="LblSlNo" runat="server" Text='<%# Bind("SlNo") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="stuname" SortExpression="stuname">
                    <EditItemTemplate>
                        <asp:TextBox ID="TxtStuName" runat="server" Text='<%# Bind("stuname") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="LblStuName" runat="server" Text='<%# Bind("stuname") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField ShowHeader="False">
                    <EditItemTemplate>
                        <asp:LinkButton ID="BtnUpdate" runat="server" CausesValidation="True" CommandName="Update"

                            Text="Update" onclick="BtnUpdate_Click"></asp:LinkButton>
                        &nbsp;<asp:LinkButton ID="BtnCancel" runat="server" CausesValidation="False" CommandName="Cancel"

                            Text="Cancel"></asp:LinkButton>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="BtnEdit" runat="server" CausesValidation="False" CommandName="Edit"

                            Text="Edit"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:LinkButton ID="BtnDelete" runat="server" CausesValidation="False" CommandName="Delete"

                            Text="Delete"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        </asp:GridView>



我的.vb文件是



And my .vb file is

Imports System.Data.SqlClient

Partial Class _Default
    Inherits System.Web.UI.Page
    Dim Conn As New SqlConnection
    Dim Cmd As New SqlCommand

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Conn = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("TestingConnectionString"))
    End Sub

    Protected Sub BtnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Cmd = New SqlCommand("Update TestInfo " _
                              + "Set Stuname = " + UCase(TxtStuName.text) _
                              + "where slno = 1", Conn)
        Conn.Open()
        Cmd.ExecuteNonQuery()

    End Sub
End Class



我无法访问TxtStuName,并给出错误消息TxtStuName未声明.

我是ASP.net的真正初学者.请帮助我..

在此先感谢



I am unable to access the TxtStuName and givin me error TxtStuName is not declared.

I am a real beginner for the ASP.net Please help me..

Thanks in advance

推荐答案

希望 Link1 [ ^ ],在ASP.NET 2.0中可编辑的GridView [ Link2 [ ^ ]可能会为您提供帮助.
Hope Link1[^], Editable GridView in ASP.NET 2.0[^]article from CP and Link2[^] might help you.


我无法访问TxtStuName,并给出错误消息TxtStuName未声明

这行听起来像是在代码隐藏"中,编译器实际上没有获得您的控件,
这是bcz,您已将文本框声明到Gridview中,因此您必须首先从gridview中查找文本框,一旦获得它,则只有您可以访问TxtStuName.Text
I am unable to access the TxtStuName and givin me error TxtStuName is not declared

This line sounds like in Code Behind the compiler actually didn''t getting your Control,
this is bcz you have declare your textbox into Gridview so You have to first Find your textbox from gridview and once you got it then only you can access your TxtStuName.Text


这篇关于需要使用VB.net更新ASP.net Gridview中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆