Asp.net窗体之间传递SqlDataSource
我建了2个Webform窗体(Asp.net),一个Webform1一个Webform2。我在Webform1里面有一个SqlDataSource1和一个Button,在...
我建了2个Webform窗体(Asp.net),一个Webform1一个Webform2。我在Webform1里面有一个SqlDataSource1和一个Button,在Webform2里是一个Textbook和一个Button。请问怎样才能在Webform1里点击Button后跳转到Webform2,并且在Webform2中的Textbook里输入值后点击form2的Button可以把值添加到Webform1里的SqlDataSource1并关闭Webform2?PS:已经绑定好了数据源,并且成功链接数据库,就是不会在Webform2里调用Webform1里的SqlDataSource1的数据。
这是Webform1的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace 考试
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("WebForm2.aspx");
Server.Transfer("WebForm2.aspx");
}
}
}
这是Webform2的代码:
public partial class WebForm2 : System.Web.UI.Page
{
DataClasses1DataContext db = new DataClasses1DataContext();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data source=.;Initial catalog=新闻;Integrated Security=true");
string strsql = "insert into 新闻(新闻页) values('" + this.TextBox1.Text + "') ";
SqlCommand cmd = new SqlCommand(strsql, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
this.GridView1.DataBind();
}
}
}
请问该怎么改,求大神
目前我把SqlDataSource1放在Webform2里面了,其实是要在Webform1里的 展开
这是Webform1的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace 考试
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("WebForm2.aspx");
Server.Transfer("WebForm2.aspx");
}
}
}
这是Webform2的代码:
public partial class WebForm2 : System.Web.UI.Page
{
DataClasses1DataContext db = new DataClasses1DataContext();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data source=.;Initial catalog=新闻;Integrated Security=true");
string strsql = "insert into 新闻(新闻页) values('" + this.TextBox1.Text + "') ";
SqlCommand cmd = new SqlCommand(strsql, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
this.GridView1.DataBind();
}
}
}
请问该怎么改,求大神
目前我把SqlDataSource1放在Webform2里面了,其实是要在Webform1里的 展开
展开全部
前台:
<asp:SqlDataSource ID="SqlDataSource_userInfo_order" runat="server"
ConnectionString="Data Source=(local);Initial Catalog=wmw;User ID=sa;Password=sa"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT * From Orders where UserID=@UserID">
<SelectParameters>
<asp:Parameter Name="UserID" Type="Int32" DefaultValue="0" />
</SelectParameters>
</asp:SqlDataSource>
后台:设置台数值SqlDataSource_userInfo_order.SelectParameters["UserID"].DefaultValue=Convert.ToInt32(Session["UserID"])
<asp:SqlDataSource ID="SqlDataSource_userInfo_order" runat="server"
ConnectionString="Data Source=(local);Initial Catalog=wmw;User ID=sa;Password=sa"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT * From Orders where UserID=@UserID">
<SelectParameters>
<asp:Parameter Name="UserID" Type="Int32" DefaultValue="0" />
</SelectParameters>
</asp:SqlDataSource>
后台:设置台数值SqlDataSource_userInfo_order.SelectParameters["UserID"].DefaultValue=Convert.ToInt32(Session["UserID"])
追问
看不懂啊,请问我应该在Webform2的Button里添加什么代码啊?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询