使用字符串变量

时间:2017-09-21 09:14:58

标签: c# variables

我的计划的某些部分。我想添加" TiklananDugme"和#34; SeciliNesne"变量到TextBox1。

我想展示" TiklananDugme"和#34; SeciliNesne"变量值在文本框中。 当我选择(在单击ImageButton1之后)GridView4中的任何行。 TextBox1仅显示" 11"

这里有什么问题?

没有其他任何人使用这些变量。(因为我没有把我的代码的另一部分放在其中)

using System;
using System.Configuration;
using System.Data.OleDb;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace OTS_WF.Moduller.Ogretmen
{
    public partial class Odevler : Page
    {
        string VTYolu = ConfigurationManager.ConnectionStrings["DataBaglantisi1_Access"].ToString();
        string MevcutBolum = "Odevler";
        string TiklananDugme;
        string SeciliNesne;
        OleDbConnection con;
        OleDbCommand cmd;
        OleDbDataReader dr;
        int MaxId = 0;
        int k = 0; //Kayıt id için 
        string SonucDers;
        string SonucOkul;
        string SonucSinif;
        string SonucTarih;
        protected void Page_Load(object sender, EventArgs e)
        {
            //there is codes but i erase to easy understanding;
        }
    }

    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox1.Text = "11";

        if (SeciliNesne == "TextBox1")
        {
            TextBox1.Text = Server.HtmlDecode(GridView2.SelectedRow.Cells[1].Text);
        }
        if (SeciliNesne == "TextBox2") 
        {
            TextBox2.Text = Server.HtmlDecode(GridView2.SelectedRow.Cells[1].Text);
            foreach (GridViewRow row in GridView4.Rows)
            {
                TextBox innerTextBox = (TextBox)row.FindControl("TextBox4x");
                innerTextBox.Text = TextBox2.Text;
            }
            TextBox2.Focus();
        }
        if (SeciliNesne == "TextBox3")
        {
            TextBox3.Text = Server.HtmlDecode(GridView2.SelectedRow.Cells[1].Text);
            TextBox3.Focus();
        }
        TextBox1.Text+= TiklananDugme + SeciliNesne;
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        GridView2.Visible = !GridView2.Visible;
        Label12.Visible = !Label12.Visible;
        TiklananDugme = "OdevSonuc";
        SeciliNesne = "TextBox3";
        OncekiYazilanlarParametre();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

为了理解这一点,我建议你理解asp.net page life cycle

背景

当您点击ImageButton1时,您'回发'您的网页是:浏览器向服务器发送请求,页面从头开始加载,然后执行ImageButton1_Click功能。< / p>

您正在SeciliNesne = "TextBox3";事件上分配ImageButton1_Click。这实际上是如何工作的,您的网页已初始化并声明SeciliNesne变量,然后调用ImageButton1_Click事件并将SeciliNesne设置为"TextBox3"

现在调用GridView2_SelectedIndexChanged时,会重复此循环,即初始化页面并声明SeciliNesne变量,然后调用GridView2_SelectedIndexChanged事件,检查SeciliNesne是否为"TextBox3"等于SeciliNesne

请注意,此时"TextBox3"未设置为GridView2_SelectedIndexChanged,因为每次加载页面时都会再次声明。{/ p>

TextBox1.Text = "11"; // you set the value of textbox if (SeciliNesne == "TextBox1")// always false { ''''''''''''''' '''''''''''''' } if (SeciliNesne == "TextBox2") // always false { '''''''''''''' ''''''''''''' } if (SeciliNesne == "TextBox3") // always false { ''''''''''''''' ''''''''''''''' } 中会发生什么:

SeciliNesne

此时,您在客户端的表上所做的更改不可用,因此您找不到在上一次单击事件期间设置的ImageButton1_Click值。您只能在aspx页面上找到已声明的内容。

<强>解决方案

有很多方法可以做到这一点,其中一种方法是使用ViewState

我并不是说这是最好的方法,但应该能够解决您的具体问题

protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { '''''''''''''' ''''''''''''' ViewState["yourKeyHere"] = "TextBox3"; } 事件中

执行此操作:

GridView2_SelectedIndexChanged

然后在TextBox1.Text = "11"; // you set the value of textbox if (ViewState["yourKeyHere"] == "TextBox1") { ''''''''''''''' '''''''''''''' } if (ViewState["yourKeyHere"] == "TextBox2") { '''''''''''''' ''''''''''''' } if (ViewState["yourKeyHere"] == "TextBox3") { ''''''''''''''' ''''''''''''''' } 事件中

showLoader() { 
this.loading = this.loadingCtrl.create({
  content: 'Loading...'
});
this.loading.present();

  }