我希望得到我在文本框中写的内容,但总是得到“”字符串。当我在textboxT_T
中输入其他内容时,我找不到该值。
以下是Gridview
代码:
<div style="margin:0 auto;width:900px;">
<asp:Label ID="USER_header" runat="server"
Text="一.先新增用戶" CssClass="=text-center"></asp:Label>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter = "true" Width="900" OnDataBound = "OnDataBound" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="工號" SortExpression="BS_ID">
<ItemTemplate>
<asp:Label ID="lblBSID" runat="server"
Text='<%# Eval("BS_ID") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="BS_ID_tb" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="中文姓名" SortExpression="BS_NAME_CHT">
<ItemTemplate>
<asp:Label ID="lblBS_NAME_CHT" runat="server"
Text='<%# Eval("BS_NAME_CHT") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="BS_NAME_CHT_tb" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="英文姓名" SortExpression="BS_NAME_ENG">
<ItemTemplate>
<asp:Label ID="lblBS_NAME_ENG" runat="server"
Text='<%# Eval("BS_NAME_ENG") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="BS_NAME_ENG_tb" runat="server" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="部門" SortExpression="BS_DEPT">
<ItemTemplate>
<asp:Label ID="lblBS_DEPT" runat="server"
Text='<%# Eval("BS_DEPT") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="DropDownList1" Width="200" runat="server">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:LinkButton ID="btnInsert" runat="server"
onclick="lbInsert_Click" Text="新增" CommandName="Insert"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>`
以下是完整的代码:
private DataSet _dataSet;
private DataSet _dataSetHKTEL;
private DataSet _datasetHKMOBILE;
private string _value;
protected void Page_Load(object sender, EventArgs e)
{
ShowData();
GridView1.FooterRow.Visible = false;
lblinsertuser.Visible = false;
lbCancelSave.Visible = false;
//if (Session["key_pass"] == null)
//{
// Response.Write("<script>alert('YOUR ACCESS DENY! 你沒有權限進入此頁面');location.href='Default.aspx';</script>");
//}
}
private void Viewtable()
{
using (SqlConnection conn = new SqlConnection(this._connectionString))
{
string query = "SELECT TOP 2 BS_ID,BS_NAME_CHT,BS_NAME_ENG,BS_DEPT FROM BS_USER ORDER BY 1 DESC ";
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataAdapter da1 = new SqlDataAdapter(cmd);
DataTable finaldata = this.GenerateDataTable();
conn.Open();
da1.Fill(finaldata);
this._dataSet = new DataSet();
this._dataSet.Tables.Add(finaldata);
conn.Close();
da1.Dispose();
}
}
private void ViewItnlHKTel()
{
using (SqlConnection conn = new SqlConnection(this._connectionString))
{
string query = "SELECT TOP 2 DA_TEL_NO,DA_USER FROM DA_ITNL_HK_TEL ORDER BY 1 DESC ";
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataAdapter da1 = new SqlDataAdapter(cmd);
DataTable finaldata = this.GenerateTable();
conn.Open();
da1.Fill(finaldata);
this._dataSetHKTEL = new DataSet();
this._dataSetHKTEL.Tables.Add(finaldata);
conn.Close();
da1.Dispose();
}
}
private void ViewITNLHKMOBILE()
{
using (SqlConnection conn = new SqlConnection(this._connectionString))
{
string query = "SELECT TOP 2 DA_PHONE_NO,DA_USER FROM DA_HK_MOBILE ORDER BY 1 DESC ";
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataAdapter da1 = new SqlDataAdapter(cmd);
DataTable finaldata = this.GenerateMOBILE_HK();
conn.Open();
da1.Fill(finaldata);
this._datasetHKMOBILE = new DataSet();
this._datasetHKMOBILE.Tables.Add(finaldata);
conn.Close();
da1.Dispose();
}
}
private DataTable GenerateDataTable()
{
DataTable table = new DataTable();
table.Columns.Add("BS_ID", typeof(string));
table.Columns.Add("BS_NAME_CHT", typeof(string));
table.Columns.Add("BS_NAME_ENG", typeof(string));
table.Columns.Add("BS_DEPT", typeof(string));
return table;
}
private DataTable GenerateTable()
{
DataTable table = new DataTable();
table.Columns.Add("DA_USER", typeof(string));
table.Columns.Add("DA_ITNL_HK_TEL", typeof(string));
return table;
}
private DataTable GenerateMOBILE_HK()
{
DataTable table = new DataTable();
table.Columns.Add("DA_USER", typeof(string));
table.Columns.Add("DA_PHONE_NO", typeof(string));
return table;
}
private void ShowData()
{
Viewtable();
GridView1.DataSource = this._dataSet;
GridView1.DataBind();
ViewItnlHKTel();
ADD_HK_TEL.DataSource = this._dataSetHKTEL;
ADD_HK_TEL.DataBind();
ViewITNLHKMOBILE();
ADD_HK_MOBILE.DataSource = this._datasetHKMOBILE;
ADD_HK_MOBILE.DataBind();
}
protected void lbladd_Click(object sender, EventArgs e)
{
//TextBox id = GridView1.FooterRow.FindControl("BS_ID_tb") as TextBox;
////string strBS_ID = (GridView1.FooterRow.FindControl("BS_ID_tb") as TextBox).Text;
//DropDownList DEPT_ddl = GridView1.FooterRow.FindControl("DropDownList1") as DropDownList;
////string strBS_ID = BS_ID_tb.Text;
////string strBS_NAME_CHT = BS_NAME_CHT_tb.Text;
////string strBS_NAME_ENG = BS_NAME_ENG.Text;
//string strBS_DEPT = DEPT_ddl.SelectedItem.Value;
////using (SqlConnection conn = new SqlConnection(this._connectionString))
////{
//// string query = "INSERT INTO BS_USER(BS_ID , BS_NAME_CHT , BS_NAME_ENG , BS_DEPT) VALUES (@BS_ID , @BS_NAME_CHT , @BS_NAME_ENG , @BS_DEPT)";
//// SqlCommand cmd = new SqlCommand(query, conn);
//// cmd.Parameters.Clear();
//// cmd.Parameters.AddWithValue("@BS_ID", strBS_ID);
//// cmd.Parameters.AddWithValue("@BS_NAME_CHT", strBS_NAME_CHT);
//// cmd.Parameters.AddWithValue("@BS_NAME_ENG", strBS_NAME_ENG);
//// cmd.Parameters.AddWithValue("@BS_DEPT", strBS_DEPT);
//// conn.Open();
//// cmd.ExecuteReader();
//// conn.Close();
//// //Response.Redirect("");
////}
}
protected void lbInsert_Click(object sender, EventArgs e)
{
GridView1.FooterRow.Visible = true;
lblinsertuser.Visible = true;
lbCancelSave.Visible = true;
lbInsert.Visible = false;
}
protected void lbCancelSave_Click(object sender, EventArgs e)
{
GridView1.FooterRow.Visible = false;
lbInsert.Visible = true;
}
DataTable Selectindex()
{
DataTable dt = new DataTable();
try
{
using (SqlConnection conn = new SqlConnection(this._connectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT '請選擇部門' AS BS_NAME , '000' AS BS_ID UNION SELECT BS_NAME , BS_ID FROM BS_DEPT ORDER BY BS_ID ASC", conn))
{
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
}
}
}
catch (SqlException exc)
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + exc.Message + "');", true);
}
catch (Exception exc)
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + exc.Message + "');", true);
}
finally
{
}
return dt;
}
protected void OnDataBound(object sender, EventArgs e)
{
DropDownList DEPT_ddl = GridView1.FooterRow.FindControl("DropDownList1") as DropDownList;
DEPT_ddl.DataSource = Selectindex();
DEPT_ddl.DataTextField = "BS_NAME";
DEPT_ddl.DataValueField = "BS_ID";
DEPT_ddl.DataBind();
}
protected void lbSave_Click(object sender, EventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Insert",StringComparison.OrdinalIgnoreCase))
{
GridView GridView1= (GridView)sender;
TextBox BS_ID_tb = (TextBox)GridView1.FooterRow.FindControl("BS_ID_tb");
string str_BS_ID = BS_ID_tb.Text;
TextBox BS_NAME_CHT_tb = (TextBox)GridView1.FooterRow.FindControl("BS_NAME_CHT_tb");
string str_BS_NAME_CHT = BS_NAME_CHT_tb.Text;
TextBox BS_NAME_ENG_tb = (TextBox)GridView1.FooterRow.FindControl("BS_NAME_ENG_tb");
string str_BS_NAME_ENG_tb = BS_NAME_ENG_tb.Text;
DropDownList DEPT_ddl = (DropDownList)GridView1.FooterRow.FindControl("DropDownList1");
string str_DEPT_value = DEPT_ddl.SelectedItem.Value;
InsertData(str_BS_ID, str_BS_NAME_CHT, str_BS_NAME_ENG_tb, str_DEPT_value);
GridView1.ShowFooter = false;
ShowData();
GridView1.FooterRow.Visible = false;
lblinsertuser.Visible = false;
lbCancelSave.Visible = false;
}
}
private void InsertData(string BS_ID, string BS_NAME_CHT, string BS_NAME_ENG, string BS_DEPT)
{
using (SqlConnection conn = new SqlConnection(this._connectionString))
{
conn.Open();
string sql = String.Format("Insert into BS_USER VALUES('{0}','{1}','{2}','{3}')", BS_ID, BS_NAME_CHT, BS_NAME_ENG, BS_DEPT);
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
}
}
答案 0 :(得分:0)
查看状态的目的很简单:
它可以在回发中保持状态。
这就是为什么你在gridview中得到像空字符串这样的初始值的原因。设置属性EnableViewState="false"
强制通过重新绑定控件而不是从视图状态获取数据。要更多地了解视图状态,see here。