C#.net中 公用string字符串的赋值失败问题

我要做的是选择底下GridView的内容 然后点击提交按钮后 把GridView中的课程ID提交到数据库保存下来

图片说明

问题出在这个公用字符串CID1 在GRIDView中选择时 CID1 能成功记录所选的信息
(图一断点验证)

图片说明

但是点击提交按钮后CID1变成空(图二断点验证)
图片说明

之前问过类似的问题,有个前辈是说点击了asp:button后 页面重新加载 刷新CID1 经过改造我现在这些操作过程是直接选择GRIDVIEW中的内容 再点击提交 中间没有点击按钮的操作 怎么会刷新呢
然后在网上查好像是要用ispostback来做 但是ispostback 只能用在方法里使用 在我定义这个字符串的位置不能够使用

希望前辈们能教教我 如何解决这个问题 或者能教教我如何点击GRIDVIEW中的内容 如何把CELL[0]中的内容传到底下submit的方法

下面是后端代码

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class HTML_ChooseCourse : System.Web.UI.Page
{
    string CID1="";

    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //记录GridView中选择的内容,到下面提交时要用到
        int index = GridView1.SelectedIndex;


        this.LabTuesday.Text = GridView1.Rows[index].Cells[1].Text;


        CID1=GridView1.Rows[index].Cells[0].Text.ToString();


    }

    public static void Messagebox(string message)
    {
        string strScript = "<script type=\"text/javascript\" language=\"javascript\">alert('" + message + "')</script>";
        System.Web.UI.Page thisPage = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
        thisPage.ClientScript.RegisterStartupScript(thisPage.GetType(), "ShowMsgBox", strScript);
    }
    protected void BtnSubmit_Click(object sender, EventArgs e)
    {

        if ( this.LabTuesday.Text != "")
        {

            string connectionstr = "Data Source=DESKTOP-79153UB;Initial Catalog=db_StudentManage;Integrated Security=True";
            SqlConnection connection = new SqlConnection(connectionstr);
            connection.Open();
            SqlCommand command = new SqlCommand();
            command.Connection = connection;

            //将上面记录的信息记录到数据库中    上面选择GRIDVIEW中的信息 记录下的 CID1 到这个方法就无内容了
            command.CommandText = string.Format("insert into  tb_ChooseCourse  values ('{0}','{1}','周二')",
                                                CID1.ToString(),Session["ID"].ToString());

            int count1 = command.ExecuteNonQuery();


            if (count1== 1)
                Messagebox("提交成功");
            else
                Messagebox("提交失败,请重试!");

            connection.Close();
        }
        else
            Messagebox("有信息为空,请继续选课!");
    }

}

下面是前端代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ChooseCourse1.aspx.cs" Inherits="HTML_ChooseCourse" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>选课</title>
        <style>        
        .TopDiv
        {
            border: 0px solid black;
            width:100%;
            height: 60px;
            position: absolute;
            left: 20px;
            top: 20px;
            text-align:center;
        }
        .MiddleDiv {
            border: 0px solid black;
            width: 680px;
            height: 290px;
            position: absolute;
            left:320px;
            top: 180px;

        }
        .SecondDiv {
            border: 0px solid black;
            width: 894px;
            height: 50px;
            position: absolute;
            left:320px;
            top: 100px;

        }
        .btn {

        border-style: none;
            border-color: inherit;
            border-width: medium;
            background-color: #62a8ea;

            line-height: 45px;

            color: white;

            cursor: pointer;

            font-size: 16px;

            font-weight: bold;

}
    </style>
</head>
<body style="background-color:#E6E6E6">
    <form id="form1" runat="server">
    <div class="TopDiv" style="font-family:幼圆;font-size:50px">
        选课
    </div>
        <div class="SecondDiv">
            <table style="background-color:#E6E6E6">
                <tr>
                    <td><asp:Button ID="BtnTuesday" CssClass="btn" runat="server"  Text="周二" Height="50px" Width="90px" Font-Underline="False" OnClientClick="return false"  /></td>
                    <td style="font-family:幼圆;background-color:#E6E6E6;text-align:center"><asp:Label ID="LabTuesday" runat="server" Height="50px" Width="490px" BackColor="Silver" ></asp:Label></td>
                    <td><asp:Button ID="BtnSubmit" CssClass="btn" runat="server" Text="提交" Height="50px" Width="90px" OnClick="BtnSubmit_Click"/></td>

                </tr>
            </table>

        </div>
    <div class="MiddleDiv">

            <asp:GridView style="text-align:center;font-family:幼圆;" Height="370px" Width="676px" ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="CId" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" >
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>
                    <asp:BoundField DataField="CId" HeaderText="课程ID" ReadOnly="True" SortExpression="CId" />
                    <asp:BoundField DataField="CName" HeaderText="课程姓名" SortExpression="CName" />
                    <asp:BoundField DataField="TID" HeaderText="负责教师ID" SortExpression="TID" />
                    <asp:CommandField HeaderText="操作" ShowSelectButton="True" />
                </Columns>
                <EditRowStyle BackColor="#999999" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            </asp:GridView>
                <br />
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:db_StudentManageConnectionString %>" DeleteCommand="DELETE FROM [tb_Course] WHERE [CId] = @original_CId AND (([CName] = @original_CName) OR ([CName] IS NULL AND @original_CName IS NULL)) AND (([TID] = @original_TID) OR ([TID] IS NULL AND @original_TID IS NULL))" InsertCommand="INSERT INTO [tb_Course] ([CId], [CName], [TID]) VALUES (@CId, @CName, @TID)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [tb_Course]" UpdateCommand="UPDATE [tb_Course] SET [CName] = @CName, [TID] = @TID WHERE [CId] = @original_CId AND (([CName] = @original_CName) OR ([CName] IS NULL AND @original_CName IS NULL)) AND (([TID] = @original_TID) OR ([TID] IS NULL AND @original_TID IS NULL))">
                <DeleteParameters>
                    <asp:Parameter Name="original_CId" Type="String" />
                    <asp:Parameter Name="original_CName" Type="String" />
                    <asp:Parameter Name="original_TID" Type="String" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="CId" Type="String" />
                    <asp:Parameter Name="CName" Type="String" />
                    <asp:Parameter Name="TID" Type="String" />
                </InsertParameters>
                <UpdateParameters>
                    <asp:Parameter Name="CName" Type="String" />
                    <asp:Parameter Name="TID" Type="String" />
                    <asp:Parameter Name="original_CId" Type="String" />
                    <asp:Parameter Name="original_CName" Type="String" />
                    <asp:Parameter Name="original_TID" Type="String" />
                </UpdateParameters>
            </asp:SqlDataSource>                   

        </div>

    </form>
</body>
</html>

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!