none
WebForm获取选中行数值的问题 RRS feed

  • 问题

  • WebForm获取选中行数值的问题

    开发环境:
    VS2010,WebForm,.NET 4.0

    控件:
    GridView+Button

    需求:
    GridView 展示多行数据, 用户选择其中一行,点击Button,将选中行的其中2列数据写入新表;

    问题:
    写入新表后, 发现选中数据始终为空值;

    主要代码:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;

    namespace WebIMS.MeetingRoom
    {
        public partial class Frm_Meeting_Order : System.Web.UI.Page
        {
            public string select_room_name = "";
            public string select_room_adds = "";
     ..............
    ..................
            protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
            {
                int itmpid = int.Parse(Session["tmpID"].ToString());
                select_room_name = GridView1.Rows[itmpid].Cells[1].Text;//读取Gridview里所选行的第1列
                select_room_adds = GridView1.Rows[itmpid].Cells[3].Text;//读取Gridview里所选行的第3列
                Notice0.Text = select_room_name;//选中时select_room_name是有值的;
                Session.Remove("tmpID"); //清除Session
       
            }

     ..................
    .................
    string strCmd = string.Format("insert into ");
                strCmd += ("Meeting_Order(Room_Name,Order_Date,Time_Start,Time_End,Subject,Remark,Number_People,Time_Created,Name_Created,Name_ID_Created,Tel_Created,Email_Created) ");
                strCmd += ("values('" + select_room_name + "','" + Order_Date + "'");//但这时select_room_name的值不知道为什么变成空值了??;
                strCmd += (",'" + startTime + "','" + endTime + "','" + Subject.Text.Trim() + "','" + Remark.Text.Trim() + "' ");
                strCmd += (",'" + Number_People.Text.Trim() + "','" + DateTime.Now.ToString() + "','" + Session["Str_Name"] + "','" + Session["Str_ID"] + "'");
                strCmd += (",'" + Session["Str_Tel"] + "','" + Session["Str_Email"] + "' "); //创建人电话,Email
                strCmd += (")"); //SQL 语句结束

                try
                {
                    SqlConnection sqlConn_insert = new SqlConnection(DBHelper.constr); //准备写入记录
                    sqlConn_insert.Open();
                    SqlCommand sqlCmd = new SqlCommand(strCmd, sqlConn_insert);

                    if (sqlCmd.ExecuteNonQuery() > 0) //写入记录
                    {
                        //新增成功!
                  
                        Notice.Text = "会议室预约成功!会议室是:" + select_room_name;
                       
                        sqlConn_insert.Close();
    ..................
    .................

    • 已移动 Lisa Zhu 2013年1月16日 8:18 asp related
    2013年1月15日 1:06

答案

  • 你好,其實如果你是用GridView1.SelectedIndexchanged去handle,那你可以直接用GridView1.SelectedRow 去抓它的資料,
    void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
      {
        // Get the currently selected row using the SelectedRow property.
        GridViewRow row = CustomersGridView.SelectedRow;
            
        // Display the company name from the selected row.
        // In this example, the third column (index 2) contains
        // the company name.
        lblMessage = "You selected " + row.Cells[2].Text + ".";
      }

      http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.gridview.selectedindexchanged(v=vs.80).aspx

    大家一齊探討、學習和研究,謝謝!
    MCSD, MCAD, MCSE+I, MCDBA, MCDST, MCSA, MCTS, MCITP, MCPD,
    MCT, Microsoft Community Star(TW & HK),
    Microsoft MVP for VB.NET since 2003
    My MSMVP Blog

    2013年1月15日 8:43

全部回复