【问题标题】:Getting data from gridview row which is already retrived from sql db and pass it to the textbox outside gridview从已从 sql db 检索到的 gridview 行获取数据并将其传递到 gridview 外部的文本框
【发布时间】:2016-09-13 11:57:31
【问题描述】:

我在 sql db 中有名为 Country 的表,现在我在 asp 中创建了网站,并为网格视图设计,我通过网格视图从 sql 获取值,我有三个文本框“CountryID”“Name”“CountryNotes”和 ADD网页中的按钮,当我填充文本框时,它将保存到数据库并使用更新面板在同一页面本身的gridview中显示它,现在我想在gridview中编辑表格,所以当我单击gridview每一行中的编辑按钮时应该将选定的行值传递给页面顶部的 texboxes,这意味着文本框在 gridview 之外。我需要通过单击更新按钮来编辑内容并更新数据,这意味着添加按钮应该更改为更新按钮

这是我的 HTML 页面

<form id="form1" runat="server">
    <asp:ScriptManager ID="script1" runat="server"></asp:ScriptManager>
<div>

    <table align="center" style="width:50%;">
        <tr>
            <td class="auto-style6">
                <asp:Label ID="Label3" runat="server" Text="Country ID" Font-Bold="True" ForeColor="Red"></asp:Label>
            </td>
            <td class="auto-style7">
                <asp:TextBox ID="Text0" runat="server" Width="138px" ></asp:TextBox>
            </td>
            <td class="auto-style8"></td>
            <td class="auto-style9">
                &nbsp;</td>
        </tr>
        <tr>
            <td class="auto-style1">
                <asp:Label ID="Label1" runat="server" Text="Country Name" Font-Bold="True" ForeColor="Red"></asp:Label>
            </td>
            <td class="auto-style4">
                <asp:TextBox ID="Text1" runat="server" Width="137px"></asp:TextBox>
            </td>
            <td>&nbsp;</td>
            <td class="auto-style5">
                &nbsp;</td>
        </tr>
        <tr>
            <td class="auto-style1">
                <asp:Label ID="Label2" runat="server" Text="Country Notes" Font-Bold="True" ForeColor="Red"></asp:Label>
            </td>
            <td class="auto-style4">
                <asp:TextBox ID="Text2" runat="server" Width="136px"></asp:TextBox>
            </td>
            <td>&nbsp;</td>
            <td class="auto-style5">
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                <br />
                <asp:Button ID="Button1" runat="server" Text="Add" BackColor="#990000" ForeColor="White" OnClick="Button1_Click" />
            </td>
            <td >
                <br />
                </td>
            <td>&nbsp;</td>
            <td class="auto-style5">&nbsp;</td>
        </tr>

    </table>

    <br />
    <br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <Triggers>

                <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="PageIndexChanging" />

            </Triggers>
    <ContentTemplate>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="CountryID" DataSourceID="SqlDataSource1" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
        <Columns>







            <asp:BoundField DataField="CountryID" HeaderText="CountryID" InsertVisible="False" ReadOnly="True" SortExpression="CountryID" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="CountryNotes" HeaderText="CountryNotes" SortExpression="CountryNotes" />
            <asp:ButtonField ButtonType="Button" CommandName="EditRow" />

                 <asp:CommandField ShowDeleteButton="True" ShowEditButton="true"/>


        </Columns>
        <FooterStyle BackColor="White" ForeColor="#000066" />
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
        <RowStyle ForeColor="#000066" />
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#007DBB" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#00547E" />
    </asp:GridView>
        </ContentTemplate>
</asp:UpdatePanel>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ATSConnectionString %>" DeleteCommand="DELETE FROM [Country1] WHERE [CountryID] = @original_CountryID AND [Name] = @original_Name AND [CountryNotes] = @original_CountryNotes" InsertCommand="INSERT INTO [Country1] ([CountryID], [Name], [CountryNotes]) VALUES (@CountryID, @Name, @CountryNotes)" SelectCommand="SELECT * FROM [Country1]" UpdateCommand="UPDATE [Country1] SET [Name] = @Name, [CountryNotes] = @CountryNotes WHERE [CountryID] = @original_CountryID AND [Name] = @original_Name AND [CountryNotes] = @original_CountryNotes" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="original_{0}">
        <DeleteParameters>
            <asp:Parameter Name="original_CountryID" Type="Int32" />
            <asp:Parameter Name="original_Name" Type="String" />
            <asp:Parameter Name="original_CountryNotes" Type="String" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="CountryID" Type="Int32" />
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="CountryNotes" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="CountryNotes" Type="String" />
            <asp:Parameter Name="original_CountryID" Type="Int32" />
            <asp:Parameter Name="original_Name" Type="String" />
            <asp:Parameter Name="original_CountryNotes" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>

</div>
</form>

这是我的 C# 代码

public partial class atc : System.Web.UI.Page
{

    SqlConnection con;
    SqlCommand cmd;
    public void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataBind();
        }

    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        con = new SqlConnection("Data Source=RYI-SYS-004;Initial Catalog=ATS;Integrated Security=True");
        cmd = new SqlCommand("insert into Country1 (CountryID,Name,CountryNotes) values(@CountryID,@Name, @CountryNotes)", con);

        cmd.Parameters.AddWithValue("@CountryID", Text0.Text);
        cmd.Parameters.AddWithValue("@Name", Text1.Text);
        cmd.Parameters.AddWithValue("@CountryNotes", Text2.Text);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

        if (IsPostBack)
        {
            Text0.Text = "";
            Text1.Text = "";
            Text2.Text = "";
            GridView1.DataBind();
        }

    }

}

这是我现有的输出

enter image description here

谁能解决这个问题!!

【问题讨论】:

  • Text0.Text = GridView1.Rows[ROW_NUM][COL_NUM],Text;请注意,行和列都为零,顺便说一句,所以第一个是 0,第二个是 1,等等。
  • 对不起,我不知道在哪里插入您评论的那些编码,实际上我还没有尝试编辑事件代码,所以如果知道它可以正确指定它@Shannon Holsinger

标签: c# asp.net sql-server gridview


【解决方案1】:
Note - there are a few changes you;ll want to make to this code. The example I'm giving you is not meant to be optimized or generalized - it's designed to answer your specific question while changing as little of your original code as possible. There may be other errors.  

public partial class atc : System.Web.UI.Page
{

SqlConnection con;
SqlCommand cmd;
public void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        GridView1.DataBind();
    }

}


protected void Button1_Click(object sender, EventArgs e)
{
    con = new SqlConnection("Data Source=RYI-SYS-004;Initial Catalog=ATS;Integrated Security=True");
    cmd = new SqlCommand("insert into Country1 (CountryID,Name,CountryNotes) values(@CountryID,@Name, @CountryNotes)", con);

    cmd.Parameters.AddWithValue("@CountryID", Text0.Text);
    cmd.Parameters.AddWithValue("@Name", Text1.Text);
    cmd.Parameters.AddWithValue("@CountryNotes", Text2.Text);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();

    if (IsPostBack)
    {
        Text0.Text = GridView1[ROW_NUM][COL_NUM].Text;
        Text1.Text = GridView1[ROW_NUM][COL_NUM].Text;
        Text2.Text = GridView1[ROW_NUM][COL_NUM].Text;
       // GridView1.DataBind();
    }

}

}

您需要为上面的每个 ROW_NUM 和 COL_NUM 输入适当的值才能使其工作,例如 GridView1[0][0] 或 Gridview1[searchRow][4],其中 searchRow 是一个 int。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多