Yuuu1
2019-07-21 15:01
采纳率: 50%
浏览 778

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

**麻烦前辈们指点一下,我这个做的功能是点击GRIDVIEW中自带的选择功能后将选定行的第一个格子的内容赋值给上面的CID1,CID2,然后在下面有个提交功能要用到这个CID1和CID2。但是CID1,CID2的值到下面提交功能就为空。 **

图片说明

这张图到第一个断点时CD1和CD3都成功赋值

图片说明

到第二个断点时,CD2和CD4成功赋值,但是此时CD1的值为空。

图片说明

再到下面用到CD1和CD2的提交功能设定的断点时CD1和CD2的值都为空

我有试过直接赋值给CID1和CID2而不用CID3和CID4,这样的还是结果一样。
CD1和CD2的赋值到提交功能的使用中间并没有经过任何赋值的步骤。

图片说明

这是那两个BTN的前端

图片说明

这是那两个IF的筛选条件也是BTN的功能赋值

下面是后端代码:

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="";

    string CID2="";

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

       int index = GridView1.SelectedIndex;
        if (this.BtnTuesday.Enabled == false && this.BtnThursday.Enabled == true)
        {
            this.LabTuesday.Text = GridView1.Rows[index].Cells[1].Text;

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


        }
        if (this.BtnTuesday.Enabled == true & this.BtnThursday.Enabled == false)
        {
            this.LabThursday.Text = GridView1.Rows[index].Cells[1].Text;

            string CID4 = GridView1.Rows[index].Cells[0].Text.ToString();
            CID2 = CID4;

        }
        if (this.BtnTuesday.Enabled == true & this.BtnThursday.Enabled == true)
        {
            Messagebox(" 您还没选择周次");
        }

    }
    protected void BtnTuesday_Click(object sender, EventArgs e)
    {
        this.BtnTuesday.Enabled = false;
        this.BtnTuesday.Font.Underline = true;

        this.BtnThursday.Enabled = true;
        this.BtnThursday.Font.Underline = false;
    }
    protected void BtnThursday_Click(object sender, EventArgs e)
    {
        this.BtnThursday.Enabled = false;
        this.BtnThursday.Font.Underline = true;

        this.BtnTuesday.Enabled = true;
        this.BtnTuesday.Font.Underline = false;
    }
    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.LabThursday.Text != "" && 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;


            command.CommandText = string.Format("insert into  tb_ChooseCourse  values ('{0}','{1}','周一')",
                                                CID1.ToString(),Session["ID"].ToString());

            int count1 = command.ExecuteNonQuery();

            command.CommandText = string.Format("insert into  tb_ChooseCourse  values ('{0}','{1}','周二')",
                                                 CID2.ToString(), Session["ID"].ToString());

            int count2 = command.ExecuteNonQuery();
            int count3=count1+count2;

            if (count3 == 2)
                Messagebox("提交成功");
            else
                Messagebox("提交失败,请重试!");
        }
        else
            Messagebox("有信息为空,请继续选课!");
    }
    }



}

下面是前端代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ChooseCourse.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" OnClick="BtnTuesday_Click" Text="周二" Height="50px" Width="90px" Font-Underline="False" /></td>
                    <td style="font-family:幼圆;background-color:#E6E6E6;text-align:center"><asp:Label ID="LabTuesday" runat="server" Height="50px" Width="200px" BackColor="Silver" ></asp:Label></td>
                    <td><asp:Button ID="BtnThursday"  CssClass="btn" runat="server" OnClick="BtnThursday_Click" Text="周四"  Height="50px" Width="90px"/></td>
                    <td style="font-family:幼圆;background-color:#E6E6E6;text-align:center"><asp:Label ID="LabThursday" runat="server" Height="50px" Width="200px" 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" OnSelectedIndexChanging="GridView1_SelectedIndexChanging">
                <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>

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • zt13502162671 2019-07-23 11:35
    已采纳

    我想问下你的cid1 和cid2 是如何赋初始值的,看的一头雾水,最好能把所有的代码都粘贴,

    点赞 评论
  • blownewbee 2019-07-21 16:12

    你的两个if条件正好相反,所以进入下面那个cid2有了值,必然cid1没有。

    点赞 评论

相关推荐 更多相似问题