Is Possible To Set Control Properties Inside A Loop Without CodeBehind
Jan 11, 2011
Is it possible to set properties of an control inside a loop without using code behind? For example how can I set the Text property of the LinkButton inside the following loop at runtime, color being a POCO?
<table>
<%foreach (var color in GetColors()) {%>
<tr>
<td><asp:LinkButton runat="server" Text='<%# ??color.MyProperty?? %>' /></td>
</tr>
<%}%>
</table>
I need to access a control inside a repeater and change its properties. To enable it or not. I got an erorr message Object reference not set to an instance of an object. Here is my code inside a method. protected void
rptCAP_ItemDataBound(object sender, RepeaterItemEventArgs e) { LinkButton lnDel = (LinkButton)rptCap.FindControl("lnkDelete"); lnkDel.Enabled = false; //<<<<< this is where the error occur }
I have a webusercontrol with a flash control. I want to assign the movieurl from codebehind when the webusercontrol is registered in a new page.
<uc1:PopVideo ID="PopVideo1" runat="server" />
inside this webusercontrol I have a flash movie player that I can assign the movieurl in codebehind on the webusercontrol. The problem is that I need to assign the value from the new page the control is registered under. How can I access the flash control from the new page and assign the value?
I have a webpart that loads in a user control programmatically. Although the webpart has personalization properties that I can use and pass to the user control, I haven't found a way for the user control to be able to update the outer webpart's properties -- the user control knows nothing of its parent webpart except what it is given. When I try to use the [Personalizable] attribute on a property inside the user control, it doesn't work; it doesn't save anything to the personalization database. In a nutshell, I need the user control to be able to personalize its properties (e.g. for a date picker within the user control where the date has to be remembered between sessions) or to somehow be able to update its parent webpart's properties.
When I place the repeater inside a user control INSIDE ANOTHER user control and onto the page this works - the property is correctly set, if I inspect it inside the EnsureChildControls() method of the custom control the composite control's Value propertyis set to Eval("value") as intended.
so I am using a foreach loop to iterate through comments. The comment section is wrapped inside "Comments" div. My function **DeleteComment** fetches comments again once you delete a comment and rebinds it to the control. However, after you delete a comment, anytime you try to delete another comment, the commentId of the very first deleted comment would keep getting passed to **DeleteComment** function instead of the passing the commentId of the comment you are trying to delete. If you refresh the page, then you can delete ONE comment again, and the same problem if you try to delete another.
I have a div with id="myDiv" and runat="server" inside a listview with id="lvItem".I need to access the div in code behind to add width of the div at runtime.How can I access the div from codebehid using C#?
Can somebody show me a better approach of looping through a Try -Catch statement. I have something like this:
Try dbConn.Open() Catch ex As Exception errmsg = ex.Message End Try
I need to try to connect 5 times before logging an error. My first thought was to nest Try - Catch 5 levels but I didn't like that idea. I'm sure there is better solution to it.
I have a problem that when i bring the array of asp and use it in the javascript, When i use the counter (i++) and print for each index of array just like "<%=app[i] %>" and it is not return the value back to me.i solve this problem for a dayedit
So I'm trying to implement the delete selected button.. How exactly do i point to the specific checkbox for a particular row from codebehind when all 3 have the same name chkSelected? I know how to do it with a gridview, but not quite sure how if we do it with a for loop..
Can I do something like this inside btnDelete_Click?
I want to add each value of 'k' to a gridview..If I have 10 values for 'k' (count=10), then I want to display a gridview with 10 rows...How can I do this?..
This is my for loop code, it is working fine , just the problem is, if there are 10 rows , it runs for 9 rows and not for 10th time? What correction is needed?
protected void gvouter_RowDataBound1(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { int count_rows_for_saving_answers = gvouter.Rows.Count; for (int ii = 0; ii <= count_rows_for_saving_answers - 1; ii++) { Label lblsubid = (Label)gvouter.Rows[ii].FindControl("lblsubid");
I'm trying to delete a bunch of records in mysql. The record info to delete (userid, name) is stored in an arraylist. It seems to only delete the first record and gives an error ("Transaction has already been rolled back or is not pending.") afterwards. I have the transaction commit inside the loop which I think is the issue but not sure how to go further if I bring it outside of the loop. Here's the relevant bits of the code.
try { ArrayList alist = new ArrayList(); //alist gets populated with data like (12345,robot)(23456,car) here. MySqlConnection conn2 = new MySqlConnection(query_connection); conn2.Open(); MySqlCommand command2 = conn2.CreateCommand(); MySqlTransaction mytransaction; mytransaction = conn2.BeginTransaction(); command2.Transaction = mytransaction; for (int i = 0; i < alist.Count; i++) { string[] s = alist[i].ToString().Split(','); try { command2.CommandText = "DELETE from users_settings WHERE UserID=" + s[0].ToString(); command2.ExecuteNonQuery(); command2.CommandText = "DELETE from users WHERE ID=" + s[0].ToString(); command2.ExecuteNonQuery(); mytransaction.Commit(); } catch (Exception e){ mytransaction.Rollback(); } } } catch(Exception e){ }
I have a GridView, the first column it's a RadioButton template and the second it's a DropDownList Template, the number of rows depends on the user, I mean they are not static. I'm using the following JS function to get the row selected with the radiobuttons:
[Code]....
With this function I know which RadioButton was selected, now, I would like to get the DropDownList value or text of the selected row, I know I have to loop through every DropDownList to get an array of controls, but since they are something like ctl00_ContentPlaceHolder1_GVProducts_ctl02_DValsDropDown, I just don't know how to achive this through JS.
In my application I have panel which has 5 textboxes. I've written several functions for checking each of textbox if the entered value is ok (number, is this number in database, etc.). If error occurrs textbox which caused it has different color of background.
However I wanted to write (add) function which will check if one of the textboxes has the same value as other. The problem is that function is checking blank textboxes, although it shouldn't - I wrote if statement which allows checking only non-blank textboxes but it doesn't work.
ive created my own custom control, now in the code behind of the custom control file i can add Name.Click += new EventHandler(name); but on the codebehind of the page where ive used the custom control i cannot, is there anyway i can regsiter it as an available attribute to add? my code below and on the image the test codebehind click is not available, how can i get it?
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class LargeButton : System.Web.UI.UserControl { public string JSFunc { get; set; } public string SSFunc { get; set; } public string ImgName { get; set; } public string LinkText { get; set; } protected void Page_Load(object sender, EventArgs e) { Link.Attributes.Add("onclick", "javascript:"+ JSFunc + ";"); Link.HRef = "javascript:;"; Link.Title = LinkText; Img.Src = "~/App_Themes/" + Page.Theme + "/Images/Icons32/" + ImgName; Img.Alt = LinkText; //Link.ServerClick += new EventHandler(asd); } } <%@ Control Language="C#" AutoEventWireup="true" CodeFile="LargeButton.ascx.cs" Inherits="LargeButton" %> <a ID="Link" runat="server" class="MenuItemLarge" clientidmode="Static" > <img ID="Img" align="center" runat="server"/> <br /> <%= LinkText %> </a> <MB:LgButton runat="server" ID="Save" JSFunc="Save(this)" ImgName="save_32.png" LinkText="Save" />
I have a custom server control inheriting from CompositeControl. It consists of two RadioButtonlist controls that are rendered side by side in an html table. The number of ListItems in each RadioButtonList is not known until runtime. Depending on how many items each list contains, the RepeatColumns property of each list is set differently. To do this in the handler for the ItemDataBound event I need to get the count of ListItems in each RadioButtonList. How do I expose the Items.Count property of each internal RadioButtonList as a top-level property? I tried using a getter to return the count but this generates a compile-time error to the effect that the count cannot be assigned to--even though I have not created a setter.
Similarly, in the handler for the Submit event, I need to get the ClientID of each internal RadioButtonList to pass to a method that checks for data changes. How do I expose the ClientID of each component RadioButtonList as a top-level property?
I can of course index the Controls collection of the composite control to access the child control and read its properties--but that defeats the whole point of making a complex, independent custom server control.
This way buttons are rendered on design view as well. The problem is that number of buttons to render is dynamic based on data in xml file (even if the data was static, the number of button is about 12 and I don't think it is wise to copy paste the same code above 6 times...). I thought by using loop for that:
int i = 1; foreach (Button button in tb.Items) { writer.AddAttribute(HtmlTextWriterAttribute.Type, "button"); writer.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID + "butto_Foo" + i); writer.AddAttribute(HtmlTextWriterAttribute.Value, button.ButtonValue); writer.RenderBeginTag(HtmlTextWriterTag.Input); writer.RenderEndTag(); //button i++; }
tb is defined in OnLoad; On run time it works, but on design time I get error: > Error rendering control. And control is not being rendered.
I am grabbing an XML feed an throwing it into a dataset. I am looping through the datatable that is in the dataset and adding a column to the datatable (Distance) that is the result of a calculation. What I then need to do is sort the dataset on the new column (Distance) I added, but it is not working... Here is a sample of my code.