get value from ascx user control to aspx page - c#

I have two files in my project. One is user control (popup) customerpicker.ascx and one is default.aspx page. In customerpicker I have dynamically generated gridview and 'select' column with SelectButton.
What I want is this: When I click on 'select' on random row in gridview, then I like to display value from selected row immediately (like ajax) to aspx.page. How it is possible?
There is part of my code in .ascx:
public string showOnaspx { get; set; }
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
showOnaspx = row.Cells[1].Text;
e.Cancel = true;
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
}
I tried casting, get/set methods, but nothing worked for me.

Answer on that question is pretty easy.. We can save variabile in session and then read it in aspx.page wherever we want.
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
Session["sessionid"]= row.Cells[1].Text;
e.Cancel = true;
}
Feeling so stupid right now, because I spent few days to figure it out simple way to do that.

Related

How do you keep a Table Visible in ASP.NET while browsing a Calendar

I'm encountering a problem when working on our ASP.NET website:
I had a table hidden until a specified button was clicked. Once that button was clicked, the table - along with it's content - will be visible by that point-on. One of the contents of that table is a Calendar. The problem is this - whenever I switch the year in the Calendar, the table goes back to its hidden state.
I recognized this because I placed the table_Name.visible = false; property at Page_load
protected void Page_Load(object sender, EventArgs e)
{
my_Table_name.Visible = false;
}
I tried fixing it, so my first solution was this:
int counter = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (counter == 0)
{
additional_Tabe.Visible = false;
counter += 1;
}
}
My solution didn't work.
My second solution is Regular Expression
My third solution is using Drop Down Lists to individually represent Month / Day / Year
The problem with my second and third solution is that I'll have to replace all Calendar my group-mates have placed with either of my solutions which is pretty much a tedious, but doable task.
I'm just wondering if there's a way to prevent the table from going back to it's hidden state while the user switches the year of the Calendar, select a date, or generally plays around with the Calendar.
Using the Page.IsPostBack property inside of Page_Load, https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.page.ispostback?view=netframework-4.8 it returns true if: the page is being loaded in response to a client postback; otherwise, false. You can wrap your my_Table_name.Visible = false; in this conditional and only set it to false if it is a postback
Code:
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
my_Table_name.Visible = false;
}
int counter = 0;
protected void Page_Load(object sender, EventArgs e)
{
if(!PostBack)
{
if (counter == 0)
{
additional_Tabe.Visible = false;
counter += 1;
}
}
}
Used this Code...

GridView select - passing values to textboxes on another form

I have a GridView with the select button turned on.
<asp:CommandField ShowSelectButton="True" />
I want to pass the row values to TextBoxes that are on another page. So I am just working with on to test with the moment.
I am currently using the following code.
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string Property_Name;
Property_Name = GridView1.SelectedRow.Cells[4].Text;
Session["Property_Name"] = Property_Name;
CreateSurvey CS = new CreateSurvey();
CS.PropDetails();
Response.Redirect("CreateSurvey.aspx");
This is my code from the second page (CreateSurvey.aspx)
public void PropDetails()
{
var Property_Name = Session["Property_Name"];
Create_PropName.Text = Property_Name.ToString();
}
The "CreateSurvey.aspx" page opens but the Create_PropName TextBox is empty.
Am I missing something?
try this
if you wan't to use session
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string Property_Name;
Property_Name = GridView1.SelectedRow.Cells[4].Text;
Session["Property_Name"] = Property_Name;
//you don't need this as you already set session above
//CreateSurvey CS = new CreateSurvey();
//CS.PropDetails();
Response.Redirect("CreateSurvey.aspx");
}
while receiving you just need to call below code in page load
if(Session["Property_Name"] != null)
Create_PropName.Text = Session["Property_Name"].ToString();
if you want to use query string
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("CreateSurvey.aspx?PropName="+GridView1.SelectedRow.Cells[4].Text);
}
in second page load
if(Request.QueryString["Property_Name"] != null)
Create_PropName.Text = Request.QueryString["Property_Name"];

One GridView, two DataSources

So, I have one GridView and 2 DataSources created using "wizard" ( GridViewTasks-->NewDataSource)
I have one CheckBox on my page...when checked=true I want to use DataSource1 for my GridView, and when checked=false DataSource2.
I have tried add CheckBox_CheckedChanged event in my code-behind something like this:
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox.Checked == true)
{
gvPredbiljezbe.DataSource = dsGridView1;
gvPredbiljezbe.DataBind();
}
else
{
gvPredbiljezbe.DataSource = dsGridView2;
gvPredbiljezbe.DataBind();
}
}
But this doesn't work.
Any suggestions?
I know I can go in my code-behind and do it all "manually" (SqlDataConnection->DataAdapter->DataTable->GridViewDataSource) but is there a way when you create your DataSources with GridView wizard and on CheckBox or ButtonClick event change your GridView's DataSource's?
Thanks
Best
K
Make sure to set AutoPostBack="true" for your checkbox.
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkbox = (CheckBox)sender;
if (checkbox != null)
gvPredbiljezbe.DataSourceID = checkbox.Checked ? dsGridView1.ID : dsGridView2.ID;
}
I used to use this technique; don't assign the DatasourceID, then set it in page_init for the initial data load, then do:
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox.Checked == true)
{
gvPredbiljezbe.DataSourceID = dsGridView1.ID;
gvPredbiljezbe.DataBind();
}
else
{
gvPredbiljezbe.DataSourceID = dsGridView2.ID;
gvPredbiljezbe.DataBind();
}
}
Something like that; swapping the ID of the datasource worked without issue (been a little while so I think you use ID, not ClientID property).

Why is my gridview rebound after selected index change?

I have a Gridview that is populated through SQLDataSource. The query behind is rather complex and the GridView takes some seconds to get filled; that's why I get annoyed by the fact that every time I select a row, the Gridview disappears for a while and is getting repopulated again. What does fire that rebind?
The selected row index works as Control Parameter for a second Gridview, that displays detail information on that row. There are these 2 events defined for the gridview:
protected void GridView_PURCHTABLE_OnDataBound(object sender, EventArgs e) {
if(DisplayPurchItems.Checked == false)
{
GridView_PURCHTABLE.Columns[4].Visible = false;
}
else
{
GridView_PURCHTABLE.Columns[4].Visible = true;
}
protected void GridView_PURCHTABLE_Selectedindexchanged(Object sender, EventArgs e) {
GridView_Notes.DataBind(); //this is the second gridview
}
Anyone has a clue what might cause the gridview to rebind?
Martin
Check once:Is post back.
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//binding grid
}
}

Getting data from selected datagridview row and which event?

I have a DataGridView (Selectionmode: FullRowSelect) on a windows form along with some textboxes, so what i want to do is that whenever a user selects a row(click or double_click maybe), the contents of that row must be displayed in the text boxes,
I tried out this code:
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show("CEll Double_Click event calls");
int rowIndex = e.RowIndex;
DataGridViewRow row = dataGridView1.Rows[rowIndex];
textBox5.Text = row.Cells[1].Value;
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int rowIndex = e.RowIndex;
DataGridViewRow row = dataGridView1.Rows[rowIndex];
textBox5.Text = dataGridView1.Rows[1].Cells[1].Value.ToString();// row.Cells[1].Value;
}
there are many other textboxes, but the main problem is that none of the event seems to be triggered, what event should i use to do so, or is there some property of datagrid that i might have set wrong?
Any help would be appreciated...:(
You can use SelectionChanged event since you are using FullRowSelect selection mode. Than inside the handler you can access SelectedRows property and get data from it. Example:
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView.SelectedRows)
{
string value1 = row.Cells[0].Value.ToString();
string value2 = row.Cells[1].Value.ToString();
//...
}
}
You can also walk through the column collection instead of typing indexes...
You can try this click event
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
Eid_txt.Text = row.Cells["Employee ID"].Value.ToString();
Name_txt.Text = row.Cells["First Name"].Value.ToString();
Surname_txt.Text = row.Cells["Last Name"].Value.ToString();
First take a label.
set its visibility to false, then on the DataGridView_CellClick event write this
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
label.Text=dataGridView1.Rows[e.RowIndex].Cells["Your Coloumn name"].Value.ToString();
// then perform your select statement according to that label.
}
//try it it might work for you
You should check your designer file. Open Form1.Designer.cs and
find this line:
windows Form Designer Generated Code.
Expand this and you will see a lot of code. So check Whether this line is there inside datagridview1 controls if not place it.
this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);
I hope it helps.
Simple solution would be as below. This is improvement of solution from vale.
private void dgMapTable_SelectionChanged(object sender, EventArgs e)
{
int active_map=0;
if(dgMapTable.SelectedRows.Count>0)
active_map = dgMapTable.SelectedRows[0].Index;
// User code if required Process_ROW(active_map);
}
Note for other reader, for above code to work FullRowSelect selection mode for datagridview should be used. You may extend this to give message if more than two rows selected.
You can use the SelectionChanged Event.
CurrentRow.DataBoundItem will give the bound item.
SelectionMode property should be full row select.
var item = ([CastToBindedItem])dataGridLocations.CurrentRow.DataBoundItem;
tbxEditLocation.Text = item.Name;

Categories