|
|
|
Findcontrol ของtextbox แล้วมันไม่เจอค่าแต่มันเจอ TextBoxSystem.Web.UI.WebControls.TextBox ครับ |
|
|
|
|
|
|
|
ผลลองใช้Gridview ในการ edit update delete ครับ ตอนนี้ทำได้เกือบครับแล้วเหลือแต่ Update ครับ คือผมFind Controlของtestbox ผมก็ตั้งชื่อตรงกะ TextBox แล้วน่ะครับ แต่พอรันให้มันFind มันก็เจอคำนี้ขึ้นมาครับ TextBoxSystem.Web.UI.WebControls.TextBox มันไม่ยอมเอาค่าจาก text box ขึ้นมาครับ ไม่ทราบว่าต้องแก้ยังไงครับขอบคุณครับ
Code (C#)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GdvGuoupType" runat="server" AutoGenerateColumns="False"
onrowdatabound="GdvGroupType_RowDataBound"
onpageindexchanging="ShowPageCommand"
onrowdeleting="DeleteRow"
DataKeyNames="GROUPTYPE_ID"
onrowediting="GdvEdit"
onrowupdating="GdvUpdate" onrowcancelingedit="CancelEdit">
<Columns>
<asp:TemplateField HeaderText="GROUPTYPE_ID">
<ItemTemplate>
<asp:Label ID="lblGroupID" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblEditGRoID" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.GROUPTYPE_ID") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GROUPTYPE_NAME_TH">
<ItemTemplate>
<asp:Label ID="lblGroupTypeNameTH" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditGrNameTH" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.GROUPTYPE_NAME_TH") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GroupTypeNameEN">
<ItemTemplate>
<asp:Label ID="lblGroupTypeNameEN" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditGrNameEN1" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.GROUPTYPE_NAME_EN") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CreateBy">
<ItemTemplate>
<asp:Label ID="lblCreateBy" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditCreate" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CREATEBY") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="true" HeaderText="Delete" />
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Code (C#)
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
public partial class _Default : System.Web.UI.Page
{
string strConnString = "Data Source=orcl_192.168.1.107;User Id=gps2;Password=gps2;";
OracleConnection objConn = new OracleConnection();
OracleCommand objCmd = new OracleCommand();
string strSQLGrid = "SELECT GROUPTYPE_ID,GROUPTYPE_NAME_TH,GROUPTYPE_NAME_EN,CREATEBY FROM GROUP_TYPE";
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
void BindData()
{
try
{
if (objConn.State == ConnectionState.Open)
{
objConn.Close();
}
OracleDataAdapter DtAdapter = new OracleDataAdapter();
DataSet ds = new DataSet();
objConn.ConnectionString = strConnString;
objCmd.Connection = objConn;
objCmd.CommandText = strSQLGrid;
objCmd.CommandType = CommandType.Text;
DtAdapter.SelectCommand = objCmd;
DtAdapter.Fill(ds, "GroupType1");
GdvGuoupType.DataSource = ds;
GdvGuoupType.DataBind();
DtAdapter = null;
objConn.Close();
}
catch
{
Response.Write("Can not Bind Data..!");
}
finally
{
objConn.Close();
}
}
protected void GdvGroupType_RowDataBound(object sender, GridViewRowEventArgs e)
{
//****GROUPTYPE_ID****//
Label lblGROUPTYPE_ID = (Label)(e.Row.FindControl("lblGroupID"));
if (lblGROUPTYPE_ID != null)
{
lblGROUPTYPE_ID.Text = (string)DataBinder.Eval(e.Row.DataItem, "GROUPTYPE_ID");
}
//****GROUPTYPE_NAME_TH****//
Label lblGROUPTYPE_NAME_TH = (Label)(e.Row.FindControl("lblGroupTypeNameTH"));
if (lblGROUPTYPE_NAME_TH != null)
{
lblGROUPTYPE_NAME_TH.Text = (string)DataBinder.Eval(e.Row.DataItem, "GROUPTYPE_NAME_TH");
}
//****GROUPTYPE_NAME_EN****//
Label lblGROUPTYPE_NAME_EN = (Label)(e.Row.FindControl("lblGroupTypeNameEN"));
if (lblGROUPTYPE_NAME_EN != null)
{
lblGROUPTYPE_NAME_EN.Text = (string)DataBinder.Eval(e.Row.DataItem, "GROUPTYPE_NAME_EN");
}
//****CREATEBY****//
Label lblCREATEBY = (Label)(e.Row.FindControl("lblCreateBy"));
if (lblCREATEBY != null)
{
lblCREATEBY.Text = (string)DataBinder.Eval(e.Row.DataItem, "CREATEBY");
}
}
protected void ShowPageCommand(object sender, GridViewPageEventArgs e)
{
GdvGuoupType.PageIndex = e.NewPageIndex;
BindData();
}
protected void DeleteRow(object sender, GridViewDeleteEventArgs e)
{
string SQLDelete = "DELETE FROM GROUP_TYPE WHERE GROUPTYPE_ID='" + GdvGuoupType.DataKeys[e.RowIndex].Value + "'";
objConn.ConnectionString = strConnString;
objCmd.Connection = objConn;
objCmd.CommandText = SQLDelete;
objCmd.CommandType = CommandType.Text;
objConn.Open();
objCmd.ExecuteNonQuery();
GdvGuoupType.EditIndex = -1;
BindData();
objConn.Close();
}
protected void GdvEdit(object sender, GridViewEditEventArgs e)
{
GdvGuoupType.EditIndex = e.NewEditIndex;
BindData();
}
protected void GdvUpdate(object sender, GridViewUpdateEventArgs e)
{
//****GroupID****//
Label lblGroupID = (Label)GdvGuoupType.Rows[e.RowIndex].FindControl("lblEditGRoID");
//****GROUPTYPE_NAME_TH****//
TextBox txtEditGrNameTH = (TextBox)GdvGuoupType.Rows[e.RowIndex].FindControl("txtEditGrNameTH");
Response.Write(txtEditGrNameTH);
//****GROUPTYPE_NAME_EN****//
TextBox txtGrNameEN = (TextBox)GdvGuoupType.Rows[e.RowIndex].FindControl("txtEditGrNameEN1");
Response.Write(txtGrNameEN);
//****CREATEBY****//
TextBox txtCreate = (TextBox)GdvGuoupType.Rows[e.RowIndex].FindControl("txtEditCreate");
string strUpdate = "UPDATE GROUP_TYPE SET GROUPTYPE_ID='" + lblGroupID.Text + "'" +
" ,GROUPTYPE_NAME_TH='" + txtEditGrNameTH.Text + "',GROUPTYPE_NAME_EN='" + txtGrNameEN.Text + "'" +
", CREATEBY='" + txtCreate.Text + "'WHERE GROUPTYPE_ID='" + GdvGuoupType.DataKeys[e.RowIndex].Value + "'";
//objCmd = new OracleCommand(strUpdate, objConn);
objConn.ConnectionString = strConnString;
objCmd.Connection = objConn;
objCmd.CommandText = strUpdate;
objCmd.CommandType = CommandType.Text;
objConn.Open();
objCmd.ExecuteNonQuery();
GdvGuoupType.EditIndex = -1;
BindData();
}
protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
{
GdvGuoupType.EditIndex = -1;
BindData();
}
}
Tag : - - - -
|
|
|
|
|
|
Date :
2010-02-20 11:39:29 |
By :
cyberwizard |
View :
1424 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จะเอาค่าจาก textbox ให้ใช้ property textbox.text
ถ้าเอา textbox ไปอ้างอิงมันจะกลายเป็น System.Web.UI.WebControls.TextBox
|
|
|
|
|
Date :
2010-02-22 08:33:03 |
By :
tungman |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|