在两页之间传递值并输入数据库 [英] passin values between two pages and entering into the databse

查看:56
本文介绍了在两页之间传递值并输入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是asp的新手.我使用了post方法将值从一页传递到另一页.
我的第一页de.aspx和我的第二页d2.aspx.从第一页开始,我将值传递到第二页,并将其显示在第二页的文本框中.我无法从那里将其输入数据库.

de.aspx


hi iam new to asp. i used the post method to pass value from one page to the another.
my first page de.aspx and my second page d2.aspx. from the first page i passed the value to the second page and displayed it in the textbox of the second page . from there iam not able to enter it into the database.

de.aspx


protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = GridView1.SelectedRow;
    string s = GridView1.SelectedRow.Cells[1].Text;
    String d = GridView1.SelectedRow.Cells[2].Text;
    Response.Redirect("Data1.aspx?code="+s+"&name="+d);
        
}


第二页d2.aspx





second page d2.aspx




protected void Button1_Click(object sender, EventArgs e)
{
   try
   {
       TextBox3.Text = TextBox1.Text;
       TextBox4.Text = TextBox2.Text;        
        SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=me;User ID=sa;Password=windows");
        SqlCommand c = new SqlCommand("INSERT INTO [deptab](depcode,depname)values(@code,@name)", con);
        con.Open();
        c.Parameters.AddWithValue("@code", TextBox3.Text);
        c.Parameters.AddWithValue("@name", TextBox4.Text);
        c.ExecuteNonQuery();
        con.Close();
    }
    catch (Exception el)
    {
        el.GetBaseException();
    }   
}

推荐答案

de.aspx,您需要将值传递给d2.aspx
From de.aspx, you need to pass values to d2.aspx
Response.Redirect("d2.aspx?code="+s+"&name="+d);



d2.aspx中,您需要在Page_Load事件中获取这些值



In d2.aspx, you need to get those values in Page_Load event

if (!Page.IsPostBack)
{
  TextBox2.Text = Page.Request.QueryString["name"];
  TextBox1.Text = Page.Request.QueryString["code"];
}



然后单击按钮即可将其与代码一起保存.



and then on button click you can save it with your code.


在操纵两个页面之间的通用数据时,可以使用全局数据容器(即全局变量之类的东西).这是处理传递的参数的简单方法.
On manipulating the common data between two pages, you can use the global data container (i.e. some thing like global variable). Itz the easy way to play around the passed arguments.


亲爱的朋友,

您将值从一页传递到另一页,但未将值绑定到控件的文本框.

因此,在页面中加载

Dear Friend,

Your Passing the Values From One Page to Another Page But your not Binding the Values to TextBox that is Controls.

So,in page Load

TextBox2.Text  = Request.QueryString["name"].ToString();

  TextBox1.Text = Request.QueryString["Code"].ToString();


然后,您可以将值插入数据库.


问候,

Anilkumar.D


Then you can insert the values in Your DB.


Regards,

Anilkumar.D


这篇关于在两页之间传递值并输入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆