ErdincDonmez ErdincDonmez - 26 days ago 5
C# Question

Using string variables

Some part of my program. i want to add "TiklananDugme" and "SeciliNesne" variables in to the TextBox1.

I want to show "TiklananDugme" and "SeciliNesne" variables values in the text box.
When i selected (after clicking ImageButton1) any row from GridView4. TextBox1 shows only "11"

What is wrong here?

There isn'n any other using place these variables.(Because of i didn't put another part of my code)

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();
}
}
}

Answer Source

In order to understand this i advice you to understand asp.net page life cycle.

Background

When you click ImageButton1 you 'postback' your webpage that is : a request is sent by browser to the server and the page is loaded from scratch and after that your ImageButton1_Click function gets executed.

You are assigning SeciliNesne = "TextBox3"; on ImageButton1_Click event. How this actually works is your page is initialized and SeciliNesne variable is declared , then your ImageButton1_Click event is called and SeciliNesne is set to "TextBox3"

Now when GridView2_SelectedIndexChanged is called this cycle is repeated i.e.your page is initialized and SeciliNesne variable is declared , then your GridView2_SelectedIndexChanged event is called you check if SeciliNesne is equal to "TextBox3"

Please note that at this point SeciliNesne is not set to "TextBox3" because every time page loads it gets declared again.

In GridView2_SelectedIndexChanged what happens is :

TextBox1.Text = "11"; // you set the value of textbox

if (SeciliNesne == "TextBox1")// always false
{
    '''''''''''''''
    ''''''''''''''

}
if (SeciliNesne == "TextBox2") // always false
{
   ''''''''''''''
   ''''''''''''' 

}
if (SeciliNesne == "TextBox3") // always false
{
    '''''''''''''''
    '''''''''''''''
}

At this point of time the changes you made on table on client side are NOT available and hence you DO NOT find SeciliNesne value which was set during previous click event. You will only find what's been declared on aspx page.

Solution

There are a number of ways to do this , one of them is to use ViewState

I am not claiming this is the best way to do it, but should be able to solve your specific problem

in your ImageButton1_Click event do this :

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

Then in your GridView2_SelectedIndexChanged event

TextBox1.Text = "11"; // you set the value of textbox

if (ViewState["yourKeyHere"]  == "TextBox1")
{
    '''''''''''''''
    ''''''''''''''
}
if (ViewState["yourKeyHere"] == "TextBox2") 
{
   ''''''''''''''
   ''''''''''''' 

}
if (ViewState["yourKeyHere"]  == "TextBox3") 
{
    '''''''''''''''
    '''''''''''''''
}