I ran into a new problem on my project;
I have a panel on my page in which I add labels, radiobuttons and 2 imagebuttons using pure programming code (because the number of labels and radiobuttons can be different).
I gave each radiobutton a special ID so I know in which content of the panel it belongs.
Now the big problem is I don't know how to get things from the panel.
Let's say the panel was filled with labels and radiobuttons (labels for questions and radiobuttons for the answering score 0-10) and I scored every question as asked, how do I for example select every radiobutton from the panel with an id that ends with '5' and get it's value ?
Name of the panel = pnlMain
my code : http://pastebin.com/gv8ycMY4
Thanks
I Hope you guys could help me out here because I'm really stuck on this.
grtz,
Nico
You could itterate through the panel's controls to get each radiobuttonlist, then itterate through those controls to get the radio buttons.
foreach (Control RBL in pnlMain.Controls)
{
if (RBL is RadioButtonList)
{
foreach (ListItem LI in (RBL as RadioButtonList).Items)
{
if (LI.Text.EndsWith("5") && LI.Selected)
{
// Do something with the radiobutton
}
}
}
}
Found the answer finally;
If you want to keep these dynamic controls after postback etc :
F.E. me, i used a SelectedIndexChange to fill up my panel with dynamic controls.
I made another method named laden() in wich i wrote my code to display these dynamic controls.
If you want these controls to pass postbacks etc simply place the following peace of code into the Page_Load method:
if (Page.IsPostBack) {
laden();
}
Wich means, if you get a postback, it will Re-load these controls after postback.
Even if you got data inserted in a textbox or radiobuttons wich where selected, it will still be the same as before the postback, no data lost.
enjoy.
Related
I am trying to remove the links that are displayed in a Telerik RadGrid by default. Here is what the grid looks like before I try to remove the edit link:
I have found this snippet of code, it is used to remove the edit link:
if (!IsPostBack)
{
foreach (GridItem item in RGV_POI.MasterTableView.Items)
{
if (item is GridEditableItem)
{
GridEditableItem editableItem = item as GridDataItem;
editableItem.Edit = true;
}
}
RGV_POI.Rebind();
}
This is how the grid looks after the code:
The edit link still shows up on the first item. Is there a way to remove the edit, update, and cancel link on each item in the RadGrid? I want to be able to remove/disable the links, using a button click event. Then be able to add/enable the links back, using a button click event.
I'm not aware of Telerik RadGrid Control, but for sure the control should inherit asp:GridView. You can make links not visible in RowDataBound event. Here how you can do it.
Add OnItemDataBound="Grid_ItemDataBound" on the grid view.
In the code behind:
protected void Grid_ItemDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Item.DataItem == null)
return;
//cell of all the link button edit/update etc.
TableCell cell = e.Item.Cells[//index of the column];
foreach(Control c in cell.Controls)
{
c.Visible = false;
}
}
You should check the ID of the cancel, edit, update buttons somehow. Probably you should give more information about the controls in the aspx.
EDIT:
Use OnItemDataBound event it existing in their documentation: http://www.telerik.com/help/aspnet-ajax/events_t_telerik_web_ui_radgrid.html
The edit link button in a RadGrid is actually a column itself, specifically a GridEditCommandColumn. In order to show/hide this in the event of a button click, you would have to essentially rebuild all the columns programatically in the click event handler, including or excluding the GridEditCommandColumn as needed. You cannot add or remove a single column programatically when the rest of the grid is created declaratively. It would be useful if we could see more of how the grid is declared and built in your application.
Creating a RadGrid Programatically
It may be possible to change the GridEditCommandColumn.Display property, however. If you can get a handle on the column itself, instead of the individual cells, you may be able to adjust this as needed in your button click events.
You should remove the GridEditCommandColumn if you do not want your items editable. Another option is to change its visibility on the server through its Visible/Display property. You can use the grid's GetColumnSafe(columnName) method to get the neded reference: http://www.telerik.com/help/aspnet-ajax/grid-using-getitems-getcolumn-methods.html
To get rid of the update/cancel buttons, you can use a custom template, although I do not see why you would need to do that if your grid is not editable: http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/form-template-update/defaultcs.aspx
suppose I have 5 check boxes. Based on number of check box checked I have to add image and hyper-link to images. (i.e if I have checked 2 check box I have to load only 2 image and link to those 2 loaded images, in-case of 3 check box checked, 3 images needs to be loaded and link to those 3 loaded images).
Is there any possible way that I can do that in asp.net using c#?
If you want to display images or hide right after you select or deselect a check box you need to set the AutoPostBack attribute of the checkboxes controls (or checkboxlist control) to true.
And in the respective OnSelectedIndexChanged event (or the Button1_Click event if that's the case) you simply change the images Visible property to true or false depeding on the options selected.
Also, if you want the user experience to be smoother you may embedd your images markup inside an UpdatePanel and call:
UpdatePanel1.Update();
Got it?
It depends on whether the CheckBoxes are in a CheckBoxList, but assuming they're not put the CheckBoxes in some type of parent container, i.e. a PlaceHolder or a Panel, and you can retrieve the CheckBoxes like this:
foreach (CheckBox chk in PlaceHolder1.Controls.OfType<CheckBox>())
{
if (chk.Checked)
{
//create/load image and hyperlink
}
}
I am dynamically adding a textbox to certain rows (one column only) of a gridview. I add the controls with this insdie of a test condition (works fine):
TextBox txtASIN = new TextBox();
txtASIN.ID = "TxtASIN" + e.Row.RowIndex;
e.Row.Cells[4].Controls.Add(txtASIN);
int i = e.Row.Cells[4].Controls.Count; //TEST: This returns 1 correctly
I want the user to be able to enter values into one or more of these textboxes and then update the database with a single button click (not one click for each row). The problem I'm having is how to access those values on a button click event. I did a simple test this way to try to see the value in the second row but get null in temp1 (I am certain there is a value entered in that textbox):
protected void btnUpdate1_Click(object sender, EventArgs e)
{
TextBox temp = (TextBox)GridView2.Rows[1].FindControl("txt1");
string temp1 = temp.Text;
int i = GridView2.Row.Cells[4].Controls.Count; //TEST: This returns 0 incorrectly }
Once I can make this work, I can iterate through the rows and do what I need to do with the values. I don't know if the text entered in the textbox is actually readable without a postback but I'm otherwise stumped. Open to better suggestions on how to do this.
Thanks.
EDIT: Here is where I am now. I can see the textboxes in my column fine. I put a break in on a button that attempts to read them and this is what I'm seeing. If I check GridView2.Rows[0].Controls.Count, I get 8, which is the correect number of columns. If I check GridVeiw2.Rows[0].Cells[4].Controls.Count, I get 0, which is wrong because the textbox is there. I can get a Count of 1 right after I dynamically create it but not when I perform a subsequent button click.
Can anyone explain this? I feel if I can get past this holdup, I can get the rest done.
Thanks again.
You need to assign an ID to the TextBox controls and then access them by that ID in FindControl(). Also, make sure you're adding the controls in the Page's Init() method of the life-cycle. That way it gets added to ViewState.
TextBox txt1 = new TextBox();
txt1.ID = "txt1";
e.Row.Cells[4].Controls.Add(txt1);
EDIT: I just remembered another possible solution. Instead of programatically creating the TextBox controls in the code-behind just create a TemplateField in the GridView.
Add Textbox TemplateField Column To GridView Programmatically
I would try a different approach, putting the text box in the html markup of the page and then control the visible or readonly property of it on the ItemDataBound event. That way, the control will always be there and you don't have to worry about the lifecycle stuff.
I have a couple GridViews that are dynamically created and placed into a PlaceHolder. When I mouse over the Select button, it shows __doPostBack('ctl00$bodyPlaceHolder$ctl0X','Select$Y'), where X = what I think is the GridView/Control index for the page and Y = row number of that GridView.
Since it is dynamically creating the GridViews, it makes sense that names them ctl0X, but on the PostBack how do I use this information?
I wouldn't even have this problem if adding the SelectedIndexChanged EventHandler worked, but it never gets called.
I found one other question like this, but the answer involved adding a GridView within my GridViews, which would also have to be dynamic, which brings me back to the original problem.
Edit
Okay, so I set gridViewDynamic.ID = "blahblah" + r.LastName, thus giving each GridView a unique name, so on mouseover in the page I get __doPostBack('ctl00$bodyPlaceHolder$blahblahSmith',Select$Y, I still can't access the items on PostBack because they no longer exist. So, I added the same GridView creation code to an if(IsPostBack), then called GridView gView = (GridView)this.Page.FindControl(blahblahSmith). Great, gView isn't null. But all the data in the rows are. Calling gView.Rows[0] returns null.
Use Page.FindControl("TheNameYouGaveTheDynamicGridView")
GridView grid = Page.FindControl("TheNameYouGaveTheDynamicGridView") as GridView;
If you are using MasterPages, you need to take a different approach to find the control on the page, but it is the same premise.
I have a GridView, each row has Edit button. After it's clicked, one of the columns turns into a drop down list where users can select value. Edit button becomes Update - so very simple usual scenario.
Now, I don't seem to be able to grab the selected drop down list after Update is clicked. Here is my code:
protected void gv_UpdateRow(string arg)
{
int currentIndex = gv.EditIndex;
gv.EditIndex = -1;
GridViewRow currentRow = gv.Rows[currentIndex];
try
{
string value2 = ((DropDownList)currentRow.FindControl("ddlValueTwo")).SelectedItem.ToString();
}
catch
{
Response.Write("error");
}
BindGridView();
}
So basically, the program execution always ends up at the catch statement. I have checked and drop down list is found, the exception is thrown when selected item is not found.
What gives?
I use c# asp.net 2.0 web forms
Looks like a databinding error, you are trying yo access data that is not present yet...
got it!
it was the IsPostback, I was missing it, so the gridview was being rebound every page load, and since the drop down list is inside the grid, the data was lost.
However, one thing I forgot to mention here is that all this code sits inside the user control (ascx file) and IsPostBack property applies to the page not the control, which is useless in my case. For example, in my circumstances I add the control manually, so IsPostback will ALWAYS be true, so to avoid this problem I had to implement a session based solution. Hope this helps someone.
There also usercontrol.IsPostBack property but it didn't perform as expected, perhaps they got it right for 3.0
First thought is that you should probably do SelectedItem.Value rather than SelectedItem.ToString().