Fill Textbox with Data from RadGrid - c#

When I click on an item I would like to fill my TextBox with numbers from a column from Grid2 after Grid1 is clicked. Right now if I click on an item in Grid1 it will then run a procedure that will fill Grid2 with data. Here is an example of the current functionality with picture attached, I click an item from Grid1 and it has 3 results, my textbox will still display as an empty textbox. Then I click another item in Grid1 and now my textbox will display the 3 results from the previously clicked item. How can I display the correct numbers in my textbox after an item is selected in Grid1.
I have tried a few different methods including:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
divDetails.Visible = true;
RadTextBox1.Text = "";
for (int i = 0; i < Grid_Product_List_Details.Items.Count; i++)
{
RadTextBox1.Text += Grid_Product_List_Details.Items[i].GetDataKeyValue("number").ToString() + "\n";
}
}
and:
protected void Grid_Product_List_Details_PreRender(object sender, EventArgs e)
{
RadGrid grid = (RadGrid)sender;
if (grid.Items.Count > 0)
{
RadTextBox1.Text = "";
}
for (int i = 0; i < grid.Items.Count; i++)
{
RadTextBox1.Text += grid.Items[i].GetDataKeyValue("number").ToString() + "\n";
}
RadTextBox1.DataBind();
}
and this:
protected void Grid_Product_List_Header_SelectedIndexChanged(object sender, EventArgs e)
{
RadTextBox1.Text = "";
for (int i = 0; i < Grid_Product_List_Details.Items.Count; i++)
{
RadTextBox1.Text += Grid_Product_List_Details.Items[i].GetDataKeyValue("number").ToString() + "\n";
}
}
But those aren't working. Any suggestions?

Try to change your code a little bit.
RadTextBox1.Text = "";
foreach (GridDataItem dataItem in Grid_Product_List_Details.Items)
{
RadTextBox1.Text += dataItem.GetDataKeyValue("number").ToString() + "\n";
}
RadTextBox1.DataBind();
This should work though I suspect your earlier code should work too.

I ended up creating a procedure which fills the textbox after an item is clicked so the most recent data appears.

Try the ItemDataBound event of the second grid. You will have access to all of its data so you can put that in the RadTextBox. I suspect you provide a data source to the second grid in the SelectedIndexChanged of the first, so its Rebind() method should be called and then you will get the ItemDataBound fired for each of its rows.
Also, if you are using AJAX - disable it. It is possible that something does not get properly updated with the partial rendering. If things work fine with full postbacks, examine the responses and see why the textbox is not included in the first request so you can know how to tweak your AJAX setup.

Related

Visibility of buttons in gridview when selecting a row ASP.net c#

Is it possible to make the edit button on the right side of my gridview table visible when selecting a row and when I select another row the previous row's edit button will disappear. Can you please help me coz I really need it to be done. Thanks!
protected void TraineeGrid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
for (int i = 0; i < TraineeGrid.Rows.Count; i++)
{
if (TraineeGrid.SelectedIndex == i)
{
TraineeGrid.Rows[i].Cells[1].Visible = true;
}
}
}

Getting IDs and values of dynamically created text boxes on Page_Load

I have a web application asp.net.
I am creating 54 text boxes dynamically in Page_Load
Here is the code
protected void Page_Load(object sender, EventArgs e)
{
for(i = 0, i<54, i++)
{
Textbox TestTextbox = new texbox();
TestTextBox.ID = "Reg" + i ;
TestTextBox.Attributes.add("runat","server");
TestTextBoxAttributes.Add("AutoPostBack", "true");
//display to a table called table1 created in the aspx page
}
}
On the page I have a button, called button1, and a on click event called "OnClickEvent", i want to capture the ID and the values of all the textboxes.
I have used Page.Controls.Count and I get only 1, the table that I have added to the aspx page, I get the IDs by using Request.Form but I`m not getting the values.
I am adding all the text boxes to a Table that I have created in the aspx file.
You can loop through the controls and check if they are of type TextBox:
for(int i = 0, i<54, i++)) {
var control = Page.FindControl("Reg" + i);
//get the value of the control
}
You are not adding the TextBox to the Page, so you cannot find it anyway. Second, adding runat=server and AutoPostBack=true as a string would not work either. (not to mention your snippet is full of errors)
//a loop uses ';', not ','
for (int i = 0; i < 54; i++)
{
//declare a new dynamic textbox = CaSe SeNsItIvE
TextBox TestTextbox = new TextBox();
TestTextbox.ID = "Reg" + i;
//if you want to add attibutes you do it like this
TestTextbox.AutoPostBack = true;
TestTextbox.TextChanged += TestTextbox_TextChanged;
//add the textbox to the page
PlaceHolder1.Controls.Add(TestTextbox);
}
And if you want to loop all the controls you can do something like this
//loop all the controls that were added to the placeholder
foreach (Control control in PlaceHolder1.Controls)
{
//is it a textbox
if (control is TextBox)
{
//cast the control back to a textbox to access it's properties
TextBox tb = control as TextBox;
string id = tb.ID;
}
}

Problems firing my event for dynamically link buttons in asp

I have problems with my dynamically link buttons in asp pages. I use them for making a custom paging for a grid view. The idea is that i want to display them 20 about 20. It is working for first 20. When i display them it's all right. Then i press next for displaying the next 20. When i press next, it displaying me, but if a press a button other than the initial 20, it is going me at the first 20.
My init page:
override protected void OnInit(EventArgs e)
{
base.OnInit(e);
if (this.Tag.Equals("Shareholder"))
{
InitComponents(false);
}
/// Must be done everytime the page loads.
InitializeList();
if (!IsPostBack)
InitializeUI();
}
Init link buttons
private void InitComponents(Boolean Prev)
{
PanelPager.Controls.Clear();
int nrInregistrari = Convert.ToInt32(DAActionar.CountActionari(11, LastBtnIndex));
if (Prev == true)
{
LinkButton lnkPrev = new LinkButton();
lnkPrev.Text = "Prev";
PanelPager.Controls.Add(lnkPrev);
}
int BtnDeAfisat = 0;
if (nrInregistrari > BTN_PER_SERIE * PAGE_SIZE)
{
BtnDeAfisat = BTN_PER_SERIE;
}
else
BtnDeAfisat = nrInregistrari / PAGE_SIZE + 1;
for (int index = 1; index <= BtnDeAfisat; index++)
{
int pageNo = index + LastBtnIndex;
LinkButton lnk = new LinkButton();
lnk.Click += new EventHandler(PageChange);
lnk.ID = "PageLink" + pageNo.ToString();
lnk.CommandName = "Page";
lnk.Text = " " + pageNo.ToString() + " ";
lnk.CommandArgument = index.ToString();
PanelPager.Controls.Add(lnk);
}
LinkButton lnkNext = new LinkButton();
lnkNext.Click += new EventHandler(NextPage);
lnkNext.Text = "Next";
PanelPager.Controls.Add(lnkNext);
LastBtnIndex += BtnDeAfisat;
}
event for next buttons
private void NextPage(object sender, EventArgs e)
{
InitComponents(true);
}
PageChange:
public void PageChange(object sender, EventArgs e)
{
int pageIndex = int.Parse((sender as LinkButton).CommandArgument) + 1;
object dataSource = GetDataSource(OwnerId, null, pageIndex);
PushData(dataSource);
}
In ASP.NET only the controls which are added as part of Page_Load will have their events attached and will be executed. Also in when a server side event is executed the page posts itself back and during that the page_load executes first and then the event handler code gets executed.
So your first set of button events executed coz they were attached to event as part of page_load. Now during event of those button you are calling InitComponents method. So as I explained during the event page_load happens first which calls InitComponents method, which adds button to panel but InitComponents method executes again as part of event handler code which removes controls from Panel and re-creates them and add them again to the Panel.
Now since controls created during 2nd execution of InitComponents method are not no part of page_load flow events attached to them are not firing when you click on them. It just posts back the page which executed InitComponents again and which creates new controls in the panel and they will have their events working fine because they are created as part of page_load flow.
Pardon me if it confuses you.
Now the solution to this would be to use a control which can create many buttons automatically for you based on the number of items and also fires events all the times.
I do not have anything working right now for you. I found this article with working sample code which you can consider as an example and implement in your work.
http://www.aspsnippets.com/Articles/Implement-Paging-in-DataList-control-in-ASPNet.aspx
The example explains how to display page numbers and also how to bind your data based on the selected page number.

Accessing Nested repeaters

Greeting, I'll try to make this short and clear
I have a NumericTextBox and a Button, upon pressing the Button it tells repeater A how many times should it repeat based on the value introduced on the NumericTextBox:
protected void ButtonRepeaterA_Click(object sender, EventArgs e)
{
string x = RadNumericTextBoxRepeatsOnA.Text.Trim();
int y = Convert.ToInt32(x);
int[] RowCount = new int[y];
RepeaterA.DataSource = RowCount;
RepeaterA.DataBind();
int currentYear = DateTime.Now.Year;
for (int i = currentYear; i <= currentYear + 100; i++)
{
//DropDownCCYear.Items.Add(new RadComboBoxItem(i.ToString(), i.ToString()));
foreach (RepeaterItem item in RepeaterA.Items)
{
RadComboBox DropDownCCYear = (RadComboBox)item.FindControl("DropDownCCYear");
DropDownCCYear.Items.Add(new RadComboBoxItem(i.ToString(), i.ToString()));
}
}
}
The issue arises with repeater B, which is inside repeater A
It behaves similarly to repeater A, but I only want the button presses to affect the repeater in which the button is inside, so it won't clear the data introduced on the controls inside the other repetitions of repeater B, which is what happens if i use a "foreach repeater" as follows:
protected void ButtonRepeaterB(object sender, EventArgs e)
{
foreach (RepeaterItem item in RepeaterA.Items)
{
Repeater RepeaterB = (Repeater)item.FindControl("RepeaterB");
RadNumericTextBox RadNumericTextBoxRepeatsOnB = (RadNumericTextBox)item.FindControl("RadNumericTextBoxRepeatsOnB");
string x = RadNumericTextBoxRepeatsOnB.Text.Trim();
int y = Convert.ToInt32(x);
int[] RowCount = new int[y];
RepeaterB.DataSource = RowCount;
RepeaterB.DataBind();
}
}
Is there a "forthis repeater"-like function?
any ideas how i can approach this?
Repeaters have an ItemCommand event that I think would be useful for you. When the Button's Click event happens, the ItemCommand event will swallow it up if you haven't set the Button's Click event handler.
On the ItemCommand event you can use the e arguments to determine which button, and the sender to determine which Repeater it came from.
I was a little confused by your question, but I think this is the answer you're driving to. Does that help?

C# WinForm Datagrid doubleclick event

Is there a doubleclick event for a datagrid? I'm trying to use this code to open a details form when the user doubleclicks on a row.
http://www.codeproject.com/KB/grid/usingdatagrid.aspx
I tried adding it by doubleclicking on the control, but it gives dataGrid1_Navigate instead.
What you get when you double click a control in design mode is the event the designers of the control thought would be the most used, in this case it's Navigate.
But yes, this control has two double click events:
public partial class Form1 : Form
{
DataGrid grid = new DataGrid();
public Form1()
{
InitializeComponent();
grid.DoubleClick += new EventHandler(grid_DoubleClick);
grid.MouseDoubleClick += new MouseEventHandler(grid_MouseDoubleClick);
grid.Dock = DockStyle.Fill;
this.Controls.Add(grid);
}
void grid_MouseDoubleClick(object sender, MouseEventArgs e)
{
}
void grid_DoubleClick(object sender, EventArgs e)
{
}
}
However, both of these events run when you double click anywhere on the control and they don't directly give you information on what row was selected. You might be able to retrieve the row double clicked in the grid_MouseDoubleClick handler by getting it from the control based on the point being clicked (e.Location), that's how it works in the TreeView control for example. At a quick glance I didn't see if the control has such a method. You might want to consider using DataGridView instead, if you don't have a particular reason to use this control.
Sounds like you need a way to get a list of all the events for a given control, rather than finding the default event (which is what VS gives you when you double click a control in the designer)
There are a few ways of doing this:
One way Select the grid.
Then click the events icon to turn the properties window into a list of events, then doubel click the event you want to strart coding the event.
Alternatively, switch to code view, select the grid in the drop down list of objects at the top left of the code window, then select the event you want from the list of all the events for that control in the event list (top right of the code window)
I tried #steve76's code, but had to tweak it slightly to work in a Windows Embedded CE 6.0 system. Here is what worked for me.
private void dataGrid1_DoubleClick(object sender, EventArgs e)
{
Point pt = dataGrid1.PointToClient(Control.MousePosition);
DataGrid.HitTestInfo info = dataGrid1.HitTest(pt.X, pt.Y);
int row;
int col;
if (info.Column < 0)
col = 0;
else
col = info.Column;
if (info.Row < 0)
row = 0;
else
row = info.Row;
object cellData = dataGrid1[row, col];
string cellString = "(null)";
if (cellData != null)
cellString = cellData.ToString();
MessageBox.Show(cellString, "Cell Contents");
}
Perhaps you can use the DataGridView.CellContentDoubleClick event.
Example:
private void DataGridView1_CellContentDoubleClick(Object sender, DataGridViewCellEventArgs e) {
System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "ColumnIndex", e.ColumnIndex );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "RowIndex", e.RowIndex );
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "CellContentDoubleClick Event" );
}
If that is not what you are looking for, you can find other events in the reference:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview_events.aspx

Categories