asp.net中如何获取textbox.value属性
我想通过打开一个新窗口选择里面的信息到一个文本框里面,然后再将选择的信息加入数据库,这就存在一个问题,对于Textbox控件,在Javascript里面是用.value属...
我想通过打开一个新窗口选择里面的信息到一个文本框里面,然后再将选择的信息加入数据库,这就存在一个问题,对于Textbox控件,在Javascript里面是用 .value属性给它赋值,但是在asp.net里面Textbox并没有.value属性,只有.text属性如何实现.value和.text属性的转变呢?
我的两个文件内容如下:
Index.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="AgeIndex" %>
<html>
<head runat="server">
<title>主页测试</title>
<link href="train/train.css" rel="stylesheet" type="text/css" />
<script language="javascript">
function opendialog()
{
var TheReturnValue=window.showModalDialog("TheDialog.aspx","","dialogWidth=450px;dialogHeight=400px;status=no;help=no;scrollbars=no")
document.form1.txtFrom.value=TheReturnValue;
}
</script>
<body>
<asp:Panel runat="server">
<asp:Label ID="lbFrom" runat="server" Text="出生地方"/>
<asp:TextBox ID="txtFrom" runat="server" Text="" ReadOnly="true"></asp:TextBox>
<input type="button" id="ChooseFrom" value="请选择" onclick="opendialog()" />
</p>
<p>
<asp:Button ID="btAdd" CssClass="word" runat="Server" OnClick="btAdd_click" Text="添加记录"/>
</p>
</asp:Panel>
Index.aspx.cs 中 btAdd_click函数内容如下:
public void btAdd_click(object sender, EventArgs e)
{
string connstr = System.Configuration.ConfigurationSettings.AppSettings["accessConn"];
string sqlstr;
sqlstr = "select * from AgePerson where Pname='" + txtName.Text + "'";
OleDbConnection conn = new OleDbConnection(connstr);
conn.Open();
OleDbDataAdapter DaSelect = new OleDbDataAdapter(sqlstr, conn);
DataSet DsSelect = new DataSet();
DaSelect.Fill(DsSelect, "t1");
if (DsSelect.Tables["t1"].Rows.Count != 0)
{
conn.Close();
Response.Write("<Script language='javascript'>alert('数据库中已经存在" + txtName.Text + "');</script>");
}
else
{
sqlstr = "INSERT INTO AgePerson(Pname,Pbirth,Pfrom)VALUES('" + this.txtName.Text + "','" + this.txtBirth.Text + "','" + this.txtFrom.Text + "')";
OleDbCommand mycmd = new OleDbCommand(sqlstr, conn);
mycmd.ExecuteNonQuery();
conn.Close();
MsgLabel.Text = "添加记录成功";
}
GridDataBind();
txtName.Text = "";
txtBirth.Text = "";
txtFrom.Text = "";
txtName.Focus();
}
在这里使用txtFrom.Text 根本得不到我想要的数据,请问如何把Javascript中赋给textbox的.value值在asp.net中取出来,小弟感激涕零了。 展开
我的两个文件内容如下:
Index.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="AgeIndex" %>
<html>
<head runat="server">
<title>主页测试</title>
<link href="train/train.css" rel="stylesheet" type="text/css" />
<script language="javascript">
function opendialog()
{
var TheReturnValue=window.showModalDialog("TheDialog.aspx","","dialogWidth=450px;dialogHeight=400px;status=no;help=no;scrollbars=no")
document.form1.txtFrom.value=TheReturnValue;
}
</script>
<body>
<asp:Panel runat="server">
<asp:Label ID="lbFrom" runat="server" Text="出生地方"/>
<asp:TextBox ID="txtFrom" runat="server" Text="" ReadOnly="true"></asp:TextBox>
<input type="button" id="ChooseFrom" value="请选择" onclick="opendialog()" />
</p>
<p>
<asp:Button ID="btAdd" CssClass="word" runat="Server" OnClick="btAdd_click" Text="添加记录"/>
</p>
</asp:Panel>
Index.aspx.cs 中 btAdd_click函数内容如下:
public void btAdd_click(object sender, EventArgs e)
{
string connstr = System.Configuration.ConfigurationSettings.AppSettings["accessConn"];
string sqlstr;
sqlstr = "select * from AgePerson where Pname='" + txtName.Text + "'";
OleDbConnection conn = new OleDbConnection(connstr);
conn.Open();
OleDbDataAdapter DaSelect = new OleDbDataAdapter(sqlstr, conn);
DataSet DsSelect = new DataSet();
DaSelect.Fill(DsSelect, "t1");
if (DsSelect.Tables["t1"].Rows.Count != 0)
{
conn.Close();
Response.Write("<Script language='javascript'>alert('数据库中已经存在" + txtName.Text + "');</script>");
}
else
{
sqlstr = "INSERT INTO AgePerson(Pname,Pbirth,Pfrom)VALUES('" + this.txtName.Text + "','" + this.txtBirth.Text + "','" + this.txtFrom.Text + "')";
OleDbCommand mycmd = new OleDbCommand(sqlstr, conn);
mycmd.ExecuteNonQuery();
conn.Close();
MsgLabel.Text = "添加记录成功";
}
GridDataBind();
txtName.Text = "";
txtBirth.Text = "";
txtFrom.Text = "";
txtName.Focus();
}
在这里使用txtFrom.Text 根本得不到我想要的数据,请问如何把Javascript中赋给textbox的.value值在asp.net中取出来,小弟感激涕零了。 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询