I'm trying to reference a query string parameter inside an HyperLink element :
<asp:HyperLink runat="server" NavigateUrl='<%# Eval(Request["id"], "~/PATH/Page.aspx?id={0}") %>' Text="reload" />
I tought it was simple... but it's not: NavigateUrl property probably fails evaluation an the resulting url is empty.
Of course Request["id"] is valid at the time the page is evaluated.
I tried also using string.format() instead Eval() with the same result.
This means <%# ... %> data-binding expression
The data-binding expression creates binding between a server control property and a data source when the control's DataBind method of this server control is called on the page.
But you are only formatting a string to set for your NavigateUrl
Inline code block is not allowed to any server tag controls
those that have runat="server"
You can only set it via code either eg: inside the Page_Load
protected void Page_Load(object sender, EventArgs e)
{
HyperLink1.NavigateUrl = string.Format("~/PATH/Page.aspx?id={0}", Request["id"]);
}
Your second post work's because it's a pure html client control and not a server control.
Source: Inline Expressions
I didn't fully understand what the problem was but the following code works:
<a href="~/PATH/Page.aspx?id=<%= Request.QueryString["id"].ToString() %>" >reload</a>
I tried several times including one using <a></a> tag with runat="server" property (not working).
Related
I have an asp.net linkButton (or imageButton) control in my profile.aspx page. I'am checking Request.Querystring("id") in the page below in the code behind.
http: //localhost:42932/profile.aspx?id=1
When I first load the profile page it is not posted back. It is ok!. When I go to another users profile (the same page just the query string is different) using imageButton control with the adddress;
http: //localhost:42932/profile.aspx?id=2
it is posted back. I dont want it to be posted back. But if I go to this page with a regular html input element like
a href = "http: //localhost:42932/profile.aspx?id=2"
it is not posted back. So I want the image button behave like an html input element.
Here is my imageButton;
ASPX:
<asp:ImageButton ID="imgProfile" ImageUrl="images/site/profile1.png" runat="server"/>
.CS
imgProfile.PostBackUrl = "profile.aspx?id=" + Session["userID"];
Edit:
if (!IsPostBack)
{
Session["order"] = 0;
}
This control is in the page load. So it should be !postback with state I mentioned above. Because all the other functions are working when Session["order"] = 0
Make use of OnCLientClick instead of OnClick, so that you only run client side code. Then, sepcify that you return false;
i.e.
<asp:ImageButton ID="imgProfile" ImageUrl="images/site/profile1.png" runat="server" OnClientClick="return false;" />
But, why use a server control, when this can be done with a normal <img .. html control?
Rather than specifying a PostBackUrl I would recommend using Response.Redirect() in the button click event handler:
public void imgProfile_Click(object sender, eventArgs e){
Response.Redirect("profile.aspx?id=" + Session["userID"]);
}
Or alternatively, just use a Hyperlink control and set the NavigateUrl property during Page_Load:
<asp:HyperLink ID="imgProfile" runat="server"><img src="images/site/profile1.png" /></asp:Hyperlink>
imgProfile.NavigateUrl = "profile.aspx?id=" + Session["userID"];
Inside repeater i have a html checkbox , now i want to access this checkbox in code behind file to check whether the checkbox is checked or not. but i dont want to user runat="server" tag , how can i do this my code as above
<input id="cbfdgroup" class="checkitem" type="checkbox" name="fd_cb_group[]" value='<%#Eval("FoodItemsUid") %>'>
in code behind file i am trying to access like this
foreach (RepeaterItem ri in rptMenu.Items)
{
if (ri.ItemType == ListItemType.Item || ri.ItemType == ListItemType.AlternatingItem)
{
HtmlInputCheckBox chk = (HtmlInputCheckBox)ri.FindControl("cbfdgroup");
if (chk.Checked)
{
}
}
}
but this is giving error as object referrence is not set to instance of an object.. how should i get this control in code behind file without using runat = "server" tag
It is not possible. To clarify, the runat="server" portion is doing just what it says. It is saying that this control should be made available and accessible to the server.
Code which is in the code-behind is code which is running on/executed by the server. So logistically, if the control is not made available to the server, the it cannot be manipulated by code (hence when it will not show up in intellisense either.)
I don't believe what you are asking is possible. runat=server is what makes controls available to the code behind. If you remove that attribute, your code behind is simply not aware of the control in any way.
A little bit more explanation:
The codebehind executes on the server. Therefore, any control you want to access in your codebehind must have runat=server in order to be available. The two are inseparable.
you don't need to read the items from repeater
assuming that you are sending a postback to the server (http post)
you can read the selected checkbox by simply:
string x = Request["fd_cb_group[]"];
it is more than 1 it will separate by comma, just use split the get a list of string of selected values.
the whole thing would be something like that:
protected void Button1_Click(object sender, EventArgs e)
{
string x = Request["fd_cb_group[]"];
string[] allSelectedOnes = x.Split(',');
foreach(string item in allSelectedOnes)
{
//your custom code for the selected checkboxes
}
}
I desingned a page with tags, Now I want to access object tags in code behind.
This is aspx page code....
<object type="label" runat="server" class="System.Web.UI.WebControls.Label" id="label_priority" parent="-1" bindedfield="priority" empty="1" value="MyValue">
Here I am adding runat=server in object tags it is giving error as
"An object tag must contain a Class, ClassID or ProgID attribute."
then I added class="System.Web.UI.WebControls.Label", now not giving any error but not showing anything in browser.
so My question is how do I access object tags in aspx.cs page?
or
I want to create a label with object tag that is accessible in code behind.
Sujeet
When you have runat="server" on a <object/> or <script/> tag, .NET expects it to be a server side object. You can however create the entire markup on the server side. Assuming you have <div id="somecontainer" runat="server"></div> in your page:
protected void Page_Load(object sender, EventArgs e)
{
HtmlGenericControl myObject = new HtmlGenericControl();
myObject.TagName = "object";
myObject.Attributes.Add("data", "somevideo.mpeg");
Page.FindControl("somecontainer").Controls.Add(myObject);
}
The result is:
<div id="somecontainer"><object data="somevideo.mpeg"></object></div>
You can use the same method to add elements inside the object, for instance <param/> tags.
You can use below like code in your page load event if you want to access this in page load :
ScriptManager.RegisterStartupScript(Page,Page.GetType(), "Script", "document.getElementById('DIVID').style.display='none'",true);
Here we are changing the display property of the div .you can change according to your requirement.
Hope this will help you.
you can do something like this
<object id="items" class = "System.Collections.ArrayList" runat="server">
as it is run at server now , you can accessing from code behind
I tried using a codeblock syntax within a property sent to a web user control:
<uc1:MyControl ID="MyControl1" runat="server" SomeProperty="<%= somevalue %>"/>
The user control has the public property SomeProperty declared and also uses code block to display the property value:
<p><% = SomeProperty %></p>
The output on my page is unfortunately
<p><%= somevalue %></p>
And not the actual value. Anyone know of some workaround for this?
You are trying to assign a server side value on a server side control - this is not possible.
You can use code blocks in client side code (that doesn't have a runat="server" attribute), this of course doesn't not apply to server side controls.
Set the attribute in the code behind (ascx), before OnRender:
// In onload, pre render or other event handler
MyControl1.SomeProperty = somevalue; // C#
MyControl1.SomeProperty = somevalue ' VB.NET
Try assigning the value of the property to a Label and call the .DataBind() method on the control.
My problem is I used to be able to do this,
< div runat="server" visible='<%#CallAFunctionThatReturnsBoolean() %>' >
and CallAFunctionThatReturnsBoolean() will be called in Page_Load when the control's DataBind function gets called implicitly and the div's visibility will be set correctly.
Now for some reason this doesn't happen anymore, and to make it work I would have to either call Page.DataBind() in my base Page class or Me.DataBind() in the Page_Load sub in that page, but I don't really want to do this, especially in the base Page class because then if I have a page with let's say a DataGrid in it that I already call the DataBind() function explicitly, then this DataGrid will get bound twice, once from Page.DateBind and once from the explicit call datagrid.DataBind().
Any idea why the control's data binding event is not called implicitly anymore?
Thanks
The <%# happens for databinding, the <%= will happen always when the page is being built reglardless of any databinding. It sounds like that is what you are looking for?
Also databinding is control level so if you 'DataBind' a grid, it will not databind any other controls. Even embedded templated controls will not be automatically databound when the grid is called unless you wire the up to do so.
Try doing the following and see if it corrects your problem:
<div runat="server" visible='<%= CallAFunctionThatReturnsBoolean() ? "true" : "false" %>' >
If you require it to occur in the databinding event, I prefer to implement OnDataBinding server side as follows:
// in your aspx
<div runat="server" OnDataBinding="yourDiv_DataBinding">
// in your .cs
protected void yourDiv_DataBinding(object sender, EventArgs e)
{
HtmlControl div = (HtmlControl)(sender);
div.Visible = CallAFunctionThatReturnsBoolean();
}