如何通过在asp.net表单之间的数据 [英] How to pass data between Forms in asp.net

查看:116
本文介绍了如何通过在asp.net表单之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用形式的DataList 复选框实现。像下面

I have form using datalist and fulfilled with checkboxes. Like below

<asp:DataList ID="DataListTest" runat="server" OnPreRender="PreTes">
        <ItemTemplate>
    <table cellpadding="0" cellspacing="0">
        <tr>
        <td>
        <asp:Label ID="lblHeader" runat="server"></asp:Label>  <!-- Telephone, Bussines System -->                                    
        </td>

        </tr>
        <tr>
        <td>
        <asp:Label ID="lblsubheader" runat="server" /></td>    <!-- Oracle, EPM, CRM (Muncul di Form) -->
        </tr>

        <tr>
        <td>

        <asp:HiddenField ID="idJenisAccess" runat="server" Value='<%# Eval("id_jenis_access") %>' />
        <asp:HiddenField ID="subhd" runat="server" Value='<%# Eval("sub_jenis") %>' /> <!-- Oracle, EPM, CRM (nama sub jenis) --> 
        <asp:HiddenField ID="id_access" runat="server" Value='<%# Eval("id_access") %>' /><!-- Output = ACT5, ACBS1 -->
        <asp:HiddenField ID="hd" runat="server" Value='<%# Eval("nama_jenis") %>' /><!-- Output = Telephone, Bussines System -->
        <asp:HiddenField ID="hdstatus" runat="server" Value='<%# Eval("status") %>' /> <!-- Output = 1 or 0 -->
        </td>
        </tr>

        <tr>
        <td>
        <asp:CheckBox ID="cbCountryName" runat="server"  Text='<%# Eval("nama_access") %>' /> <!-- Output = Local, Handphone, Project -->
        <asp:TextBox ID="testme" runat="server" Text="" Visible="false" /> <!-- Output = Textbox for all -->            
       </td>
        </tr>

        <tr>
        <td><asp:Label ID="lblReason" runat="server" Visible="false" text="Reason : "/>
        <asp:TextBox ID="txtReason" runat="server" Visible="false" />    
        </td>                   
        </tr>

        <tr>
        <td>
        <asp:Label ID="lblDescription" runat="server" Visible="false" text="Description : "/>
        <asp:TextBox ID="txtDescription" runat="server" Text="" Visible="false" />
        </td>
        </tr>
   </table>
        </ItemTemplate>
    </asp:DataList>

和背后的形式code,获取数据从数据库就是这个样子的下面。

And the code behind form, to get data from database is like this below.

private void ShowDataList()
{
    conn.Open();
    string sql = "Select access.id_access as 'id_access', access.status as 'status' , access.nama_access, jenis_access.id_jenis_access as 'id_jenis_access' ,jenis_access.nama_jenis_access as 'nama_jenis', sub_jenis.nama_sub_jenis as 'sub_jenis',sub_jenis.id_sub_jenis "+
                 "FROM access LEFT JOIN detil_access ON access.id_access = detil_access.id_access "+
                 "LEFT JOIN jenis_access ON detil_access.id_jenis_access = jenis_access.id_jenis_access "+
                 "LEFT JOIN sub_jenis ON detil_access.id_sub_jenis = sub_jenis.id_sub_jenis "+
                 "ORDER BY jenis_access.nama_jenis_access";
    SqlCommand cmd = new SqlCommand(sql, conn);
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    dt = new DataTable();
    adp.Fill(dt);
    DataListTest.DataSource = dt;
    DataListTest.DataBind();
}

这是我的 preRender Datalist中,我把它叫做 preTEST 。它也是我的code,显示数据为复选框

And this is my PreRender Datalist, I called it PreTest. And it is also my code to show the data as checkboxes.

protected void PreTes(object sender, EventArgs e)
{
    string temp = ""; 
    string subtemp ="";
    foreach (DataListItem item in DataListTest.Items)
    {
        Label objLabel = item.FindControl("lblHeader") as Label; /* Output = Telepon, Business System (nama jenis access) */
        Label subjenis = item.FindControl("lblsubheader") as Label; /* Output = Oracle, CRM, EPM (nama sub jenis access) */
        Label lblreason = (Label)item.FindControl("lblReason"); /* Output = Reason : (label reaseon) */
        Label lbldescription = (Label)item.FindControl("lblDescription"); /* Output = Description : (label Description) */
        TextBox txtOther = item.FindControl("testme") as TextBox; /* Output = textbox except reason and description */
        TextBox txtReason = item.FindControl("txtReason") as TextBox; /* Output = textbox on Reason */
        TextBox txtDescription = item.FindControl("txtDescription") as TextBox; /* Output = textbox on Description */
        CheckBox objName = item.FindControl("cbCountryName") as CheckBox; /* Output = Local, Internet, Email (nama access) */
        HiddenField objHD = item.FindControl("hd") as HiddenField; /* Output = Telpon, Business System (if deleted, nama jenis will be show up every single access name) */
        HiddenField subobjHD = item.FindControl("subhd") as HiddenField;
        HiddenField id_access = (HiddenField)item.FindControl("id_access");
        HiddenField id_jenis = (HiddenField)item.FindControl("idJenisAccess");
        HiddenField status = (HiddenField)item.FindControl("hdstatus");


        if (temp != objHD.Value)
        {                                                     
             if (objHD.Value=="Others" && id_access.Value == "ACT5")
             {
                 lblreason.Visible = true;
                 txtReason.Visible = true;
                 lbldescription.Visible = true;
                 txtDescription.Visible = true;
             }
             temp = objHD.Value;
             objLabel.Text = temp + "<br/>";
        }
        if (subtemp != subobjHD.Value)
        {
            subtemp = subobjHD.Value;
            subjenis.Text = subtemp+"<br/>";
        }
        if (status.Value == "1")
        {
            txtOther.Visible = true;                                
        }            
    }
}

毕竟,我想从表单中的数据保存到数据库中。我的意思是,用户检查一些复选框后。我婉插入所有数据库。这是我的code插入数据

And after All, I want to save the data from form into database. I mean, after user check some checkboxes. I wan to Insert all to database. and this is my code to insert the data

private void InsertActivationDetail()
{
    //Mengambil nilai idActivation
    int MaxActivationId = GetGenerateActivationID();
    //

    foreach (DataListItem objitem in DataListTest.Items)
    {

        /* To get data from the form , then insert into database */
        CheckBox cbCountryName = (CheckBox)objitem.FindControl("cbCountryName");
        HiddenField id_access = (HiddenField)objitem.FindControl("id_access");
        HiddenField id_jenis_access = (HiddenField)objitem.FindControl("idJenisAccess");
        TextBox txtOther = objitem.FindControl("testme") as TextBox;
        TextBox txtreason = objitem.FindControl("txtReason") as TextBox;
        TextBox txtdescription = objitem.FindControl("txtDescription") as TextBox;
        /* Get data end, then Inserting the data (below) */

        /*Check if datalist is null or not*/
        if (objitem !=null)
        {
            /*if not nul, then check the checkboxes, is null or not*/
            if (cbCountryName.Checked == true)
            {                    
                /*if not null, then insert the data */
                conn.Open();                    
                     sql = "INSERT INTO detil_activation (id_activation_access, id_jenis_access, id_access, " +
                          "keterangan, reason, description) " +
                          "VALUES ('" +
                          MaxActivationId + "', '"+ id_jenis_access.Value +"', '" + id_access.Value + "','"+
                          txtOther.Text +"','"+txtreason.Text+"','"+txtdescription.Text+"')";                    
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();                    
                conn.Close();
                /* Inserting data is done */ 
            }
        }            
    }       
}

我的问题是,

如何发送所有选定的复选框由用户到另一个网页表单?

how to send all selected checkboxes by user into the other web form ?

例如:

当用户检查一些复选框,然后点击提交,然后我插入其中的复选框 id_access 检查了所有数据, id_jenis ,并在文本框(ID =TESTME)到数据库中的文本。

when user check some checkboxes and click submit, then I insert all data which checked on checkbox like id_access , id_jenis , and text on textbox (ID ="testme") into database.

所有程序时会发生用户点击按钮,提交并点击时提交按钮,我想从检查复选框的所有数据是展会的其他页面(页面的名字= SendingActivati​​on)。

All process will happen when user click button submit and when submit button is clicked, I want all data from checked checkboxes are show on the other page (page's name = "SendingActivation").

如您选择:id_access id_jenis(进入)id_access id_jenis(进入)id_access id_jenis(不亚于选择数据)

like "You choose : id_access id_jenis (enter) id_access id_jenis (enter) id_access id_jenis (as much as the selected data).

FYI:我是新手在这里也是C#编程,所以请帮助我一步一步

FYI : I'm newbie here also c# programming, so please help me step by step

推荐答案

他们有很多方法都可以实现这一点;

Their are lots of methods are available to achieve this;


  • 应用程序变量

  • 会话变量

  • 的Response.Redirect

  • Cookiess

  • 使用POST方法

  • Application Variables
  • Session Variables
  • Response.Redirect
  • Cookiess
  • Using POST method

应用程序变量

传:

Application["id_access"] = "A0123";

访问:

string id_access = Application["id_access"].ToString();

会话变量

分配:

Session["id_access"] = "A0123";

阅读:

 string id_access =  Session["id_access"];

的Response.Redirect

传:

Response.Redirect("SendingActivation.aspx?id_access="A0123");

访问:

  string id_access = Request.QueryString["id_access"];

Cookiess

传:

HttpCookie id_access= new HttpCookie("Name");
id_access.Value="A0123"; 

访问:

string id_access= Request.Cookies["Name"].Value;

使用POST方法

传:

在源页面按钮当表单submitted.Set的一项PostBackUrl 属性的控制值后控制页面的URL要发布的ASP.NET Web页

In the source page button control that post values when the form is submitted.Set the PostBackUrl property for the control to the URL of the page to which you want to post the ASP.NET Web page

访问:

string id_access= Request.Form["ControlID"];

这篇关于如何通过在asp.net表单之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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