How to Concatenate an Asp.net control name with a string - c#

Basically I have a few asp.net objects. I get the ID of object 1 and assign it to a string. I then want to add a control to a placeholder than end in that ID. Not too sure how to go about doing this.
string id = Regex.Match(btnCreateHazard.ID, #"\d+$").Value;
phHazard(SOMEHOW GET THE ID HERE).Controls.Add(txtHazardDesc);
Thanks a lot.

What i understood id you want to add a string to a control that u can do like this..
string id = Regex.Match(btnCreateHazard.ID, #"\d+$").Value;
placeholderId.Controls.Add(id);
OR
can refer below example.
https://msdn.microsoft.com/en-us/library/kyt0fzt1.aspx

Related

address an object by retrieving the ID name of the object from a variable

Good evening,
I am trying to get the following done. I have seen a similar post but it was related with Unity.
Anyway, I am on web forms in asp.net and I have a radiobuttonList with ID="id001"
so on my code behind, I would normally be able to get the selected value by just doing:
string value = id001.SelectedValue
However, in this situation, I don't know the exact ID name, so I have a function that retrieves it. So now I have the variable with the name of the ID. So I want to be able to now, convert the value of that variable in something like this:
string foundid = "id001"
string foundidvalue = id001.SelectedValue
I hope this makes sense.
Thanks in advance for the help.
I am assuming this one is related to your previous question. So, when you found the control, instead of using function to get the fullname, you can do like this:
foreach (Control c in Page.Form.Controls.OfType<RadioButtonList>())
{
if (c.ID.Contains("id"))
{
// string FullID = c.ID.ToString();
var radioButtonList = c as RadioButtonList;
var selectedValue = radioButtonList.SelectedValue;
}
}
You want to use FindControl.
string foundid = "id001";
var foundCtrl = (RadiobuttonList)FindControl(foundid);
var result = foundCtrl.SelectedValue;

get the description field of a sitefinity classification in code

I am new to sitefinity, I am looking for a way to the access the description field of the classification in my code.
Please let me know how I can do that.
I have written code that gets me all the classifications(hierarchial taxonomy) in the form of a tree that I am binding to a RadTreeView control.
Each node in the RadTreeView control has properties like text, navigateURL etc. but no Description. I assume I have to do it differently to get the description field.
Any help or direction is appreciated. It looks to me like a very basic implementation to get the description but not able to get it.
Thanks!
Below is a function that uses the Sitefinity API to look up a Category by Title.
You'll need the following using statements:
using Telerik.Sitefinity.Taxonomies;
using Telerik.Sitefinity.Taxonomies.Model;
I've added the line that gets the description to hopefully better answer your question.
private Taxon GetCategoryByTitle(string category)
{
var manager = TaxonomyManager.GetManager();
var categoriesTaxa = manager.GetTaxonomy<HierarchicalTaxonomy>(TaxonomyManager.CategoriesTaxonomyId);
var taxomony = categoriesTaxa.Taxa.FirstOrDefault(t => t.Title == category);
var description = taxomony.Description; //get description here
return taxomony;
}

Textbox text as Object's name

I have a TextBox named "tb1" (Not real name).
I want to, when I click a button; create an "Product" object with the value of the "tb1" text.
Something like...
Product tb1.text = new Product();
How do I do it ?
You need to override the constructor on the Product object so that it can accept a string parameter.
Product object:
public Product(string productName)
{
//set product name variable using productName parameter
_productName = productName;
}
And then you can do something like this:
//set product name using new constructor
Product product = new Product(tb1.Text);
Pass "tb1.text" to Product's constructor while creating with new keyword.
Your example is trying to create a Product where the variable name is the content of tb1.Text. Far as I know, thats impossible. It'd be really confusing for anybody reading the code later anyway, so even if it is possible please don't do it. :)
If you're just trying to create a Product where the Product name is set to the value of tb1.Text, you can do this:
Product someProduct = new Product();
someProduct.Name = tb1.Text;
Or pass into the constructor like Arun mentioned.

Splitting a query string?

this may seem like a stupid question but I have a query string which passes a unique id across pages, What I am wondering is how I would split the following so that I have just the id?
property-buildings_e1.aspx?SubPage=561
Many Thanks!
Request.QueryString("SubPage")
int subPage = 0;
if(Request.QueryString["SubPage"] != null)
subPage = Convert.ToInt32(Request.QueryString["SubPage"]);
If you want to get the value of SubPage
Request.Params["SubPage"]
or
Request.QueryString["SubPage"]
You can use
HttpUtility.ParseQueryString.
NameValueCollection parts = HttpUtility.ParseQueryString(query);
string subPage = parts["SubPage"];
You should be able to access it using
Request.Querystring("SubPage")

Pass parameters to a user control - asp.net

I have this user control:
<user:RatingStars runat="server" product="<%= getProductId() %>" category="<%= getCategoryId() %>"></user:RatingStars>
You can see that I fill in the product and category by calling two methods:
public string getProductId()
{
return productId.ToString();
}
public string getCategoryId()
{
return categoryId.ToString();
}
I do not understand why, in the user control, when I take the data received (product and category) it gives me "<%= getProductId() %>" instead of giving the id received from that method...
Any help would be kindly appreciated...
Edit: Solved with: product='<%# getProductId() %>'
Last problem: in the user control I have this:
public string productId;
public string product
{
get
{
return productId;
}
set
{
productId = value;
}
}
So, I expect that the productId is set up ok in the user control.
Unfortunately it is null when I try to use it...
Is there anything I wrote that's incorrect?
So that you get compile-time checking, you can give your user control an ID and then set its Product and Category properties in C# like this:
ASPX:
<user:RatingStars id="myUserControlID" runat="server" Product="<%= getProductId() %>" Category="<%= getCategoryId() %>"></user:RatingStars>
CS:
myUserControlID.Product = GetProductId();
myUserControlID.Category = GetCategoryId();
Also, as 5arx mentions, once you've populated that then refreshing your page will reload your control and you'll lose the Product and Category IDs. You can handle that by using ViewState on the properties in your user control, like this:
private const string ProductKey = "ProductViewStateKey";
public string Product
{
get
{
if (ViewState[ProductKey] == null)
{
// do whatever you want here in case it's null
// throw an error, return string.empty or whatever
}
return ViewState[ProductKey].ToString();
}
set
{
ViewState[ProductKey] = value;
}
}
NOTE: I've updated the property name casing to follow convention, as it just makes more sense to me that way! Personally, I'd always suffix IDs with ID (eg: ProductID) to distinguish it from a property that contains a Product object. Read more about coding standards here: Are there any suggestions for developing a C# coding standards / best practices document?
Please post some more of your code?
A couple of things:
productId is an object reference (to a string object) which means:
unless you write some intialisation code to 'fill' it with a string reference at the start, it will be null.
if you create a string e.g. string x = new String() x will be an empty string, which you can think of as "" without the quotes. (It is a string, but its empty because it has no characters in it).
you can just write return productId; - no need to call ToString() on a string.
Your page does not have an out-of-the-box mechanism to store variables across postbacks. You need to use ViewState or hidden form fields to do this.

Categories

Resources