ASP.NET passing variables from web form to code behind - asp.net

i'm developing a web application for my company, but i'm coming up against a very simple problem that i don't now how to resolve. I search a lot on internet but i can't find anything.
I need to call a code behind method from an asp.net controls passing variables.
This is the method in the code behind (file.aspx.cs):
protected void SayHello(object sender, EventArgs e, String RandomName)
{
Response.Write(PrintedString);
}
And this is a asp.net control that call the method through OnLoad event :
<asp:Label ID="Label1" runat="server" Text="Hello" OnLoad="Visibility('ciao mamma')"></asp:Label>
What's wrong with this simple thing? Where i'm wrong?
Please answer to this simple question, it's driving me crazy...Thanks.

You can pass an argument to the event handler by the following way, but please note it will work for only asp button.
<asp:Button ID="btn" runat="server" Text="Click" OnCommand="btn_Command" CommandArgument="YourArgumentValue"/>
and then in the code behind file you do like this :
protected void btn_Command(object sender, CommandEventArgs e)
{
Response.Write(e.CommandArgument);
}
It will work for only asp button because they have a onCommand attribute with them.
whenever you will click on the button this code behind file code gets executed.
Hope this helps.

You can set the value to label on Page_Load event
You can use below code.
protected void Page_Load(object sender, EventArgs e)
{
string UserName="test";
Label1.Text="Hello "+ UserName;
}

Is this what you need?
Markup:
<asp:Label ID="lbl" runat="server"><%=SayHello("foo") %></asp:Label>
Code behind:
protected string SayHello(string name)
{
return "Hello " + name;
}

Related

Button click not firing in UserControl

I have user control deriving from BaseUserControl class and Now I need to add button in UserControl file(ascx)
I have added following code snippet in ascx:
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>
Below is the code sample of ascx.cs file
public partial class MyContol : BaseUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// code to execute after button is clicked
}
}
But here the button event is not triggering and during debug I am able to see its hitting Page_Load event, please help me to resolve on this.
Regards
Anand
I have seen this type of thing many times. Try the following.
Remove the button from the ASCX side of the page.
Remove the C# btn submit method.
Save the page.
Now go into design view...
Add the button back, assign the name in design view.
In Design View double click on the button so that it created the C# method for you.

How i can call JavaScript function from asp.net server side if script in user control

I have a ASP.NET page with 1 user controls registered.
i have one server control
<asp:LinkButton ID="vidoUpdatebtn" OnClick="vidoUpdatebtn_Click" runat="server">LinkButton</asp:LinkButton>
in .cs i handle
protected void vidoUpdatebtn_Click(object sender, EventArgs e)
{
//do something
ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "doSomeThing()", true);
}
in user control i have function doSomeThing()
function doSomeThing() {
alert("TestReady");
}
when i click on LinckButon registred function dosen't work
Write this in PageLoad (the only modifications from what you were writing are the moment in time of the registration -- PageLoad not Click, Click is too late and the face that you must write the actual implementation of the javascript function here):
protected void Page_Load(object sender, EventArgs e) {
ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript",
#"function doSomeThing() {
alert(""TestReady"");
}"
, true);
}
and this in the asp:LinkButton tag (you must specify the OnClientClick attribute):
<asp:LinkButton ID="vidoUpdatebtn" runat="server" OnClick="vidoUpdatebtn_Click" OnClientClick="doSomeThing()" >LinkButton</asp:LinkButton>
And it should work.
Have you tried this way:
Add Script manager on your aspx page
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
Codebehind:
Page.ClientScript.RegisterStartupScript(this.GetType(),"myscript", "doSomeThing()",true);
If you want client side code to run on the button click, you could use the OnClickClick property instead of the OnClick event. That'll also avoid the need for a postback and will give immediate feedback to the user.
e.g.
<asp:LinkButton ID="vidoUpdatebtn" OnClientClick="alert('TestReady'); return false;" runat="server">LinkButton</asp:LinkButton>

How to display image in img ASP control?

I want to display image in img control.
Asp code:
<asp:Image id="id" src='<%# Eval("Item.SmallPicPath") %>' Runat="Server" >
C# code:
public void Page_Load(Object sender, EventArgs e)
{
Glasses item=GetItemByID(GlassesID) //retrive record from data base
}
Glasses is a class that have property SmallPicPath.
I try to display image but i get error any idea how to fix it?
Thank you in advance!
The approach you're trying to use is based on databinding, which generally isn't good for a single image.
Instead, try something like this markup:
<asp:Image id="MyImageId" runat="server">
Then, in your code behind:
public void Page_Load(Object sender, EventArgs e)
{
Glasses item = GetItemByID(GlassesID); //retrive record from data base
this.MyImageId.ImageUrl = item.SmallPicPath;
}
If you still want to use databinding for some reason, something like this should work (although there's a performance cost compared to the other approach):
<asp:Image id="MyImageId" runat="server" ImageUrl="<%# item.SmallPicPath %>">
Code-behind:
public Glasses item;
public void Page_Load(Object sender, EventArgs e)
{
this.item = GetItemByID(GlassesID); //retrive record from data base
this.MyImageId.DataBind();
}
Databinding is an optional operation; you need to call DataBind() on the control (or its parent) to make it happen.
You only need "Eval()" in cases that involve a data container (usually via a DataSource) -- which this one doesn't.
<asp:Image id="id" ImageUrl='<%# Eval("Item.SmallPicPath") %>' Runat="Server" >
The Eval expression might not be Item.SmallPicPath. If your data object is type of Glass and this type has SmallPicPath property, then use Eval("SmallPicPath").

Viewstate lost on postback for .net Sitecore page

I'm getting some strange behaviour with viewstate being lost on postback for a .net application using Sitecore. I'm assuming it might be some config variable somewhere but I'm new to Sitecore and don't really know where to start looking.
UPDATE: Sitecore has now gotten back to us with an answer. We had recently added the dtSearch module, and AutomaticDataBind was set to true in the dtSearch.config which overrides the setting in the web config. We've now removed it and it works fine again.
I've made a mini test if that might help. It's two usercontrols on one page, both with a repeater. When updating the viewstate gets lost so even if I'm binding the updated repeater again the data for the other one will be lost.
Usercontrol 1:
<asp:Repeater runat="server" ID="Repeater1" OnItemDataBound="Repeater1_ItemBind">
<ItemTemplate>
<li>
<asp:Literal runat="server" ID="Literal1"></asp:Literal>
</li>
</ItemTemplate>
</asp:Repeater>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<string> myTestList1 = new List<string>();
myTestList1.Add("a");
myTestList1.Add("b");
Repeater1.DataSource = myTestList1;
Repeater1.DataBind();
}
}
protected void Repeater1_ItemBind(object sender, RepeaterItemEventArgs e)
{
Literal Literal1 = (Literal)e.Item.FindControl("Literal1");
Literal1.Text = (string)e.Item.DataItem;
}
Usercontrol 2:
<asp:Repeater runat="server" ID="Repeater2" OnItemDataBound="Repeater2_ItemBind" OnItemCommand="Repeater2_Command">
<ItemTemplate>
<li>
<asp:Literal runat="server" ID="Literal2"></asp:Literal>
<asp:LinkButton ID="Update" CommandName="Update" runat="server">
update
</asp:LinkButton>
</li>
</ItemTemplate>
</asp:Repeater>
private string test = String.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
test = "a";
Repeater2.DataSource = test;
Repeater2.DataBind();
}
}
protected void Repeater2_ItemBind(object sender, RepeaterItemEventArgs e)
{
char c = (char)e.Item.DataItem;
Literal Literal2 = (Literal)e.Item.FindControl("Literal2");
Literal2.Text = c.ToString();
}
protected void Repeater2_Command(object sender, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Update")
{
test = "b";
Repeater2.DataSource = test;
Repeater2.DataBind();
}
}
Does anyone have any ideas what might be going on? Let me know if I need to provide any more information. The most annoying thing is that it was working last week but I have no idea what has changed!
Thanks,
Annelie
Do you have System.Web.UI.WebControls.Repeater in the "typesThatShouldNotBeExpanded" section of your web.config?
I've found that there are definitely some things that don't work with the regular PostBack model in Sitecore... but this Repeater should be OK.
One trouble area is having FieldRenderers inside Repeaters. They don't seem to restore the Item property correctly on Postback.
As far as I can see, this thread describes exactly the same problem, but with DataGrid. See if setting AutomaticDataBind to 'false' in web.config helps.

Template variable in ASP.NET

I have to add a Page's variable into a ItemTemplate but dont know how.
For example:
<rad:RadMenuItem ID="f" runat="server" Text="Products">
<ItemTemplate>
<div class="pitem"><%= MyText %></div>
</ItemTemplate>
</rad:RadMenuItem>
The MyText variable does exist in the context and has value but the text does not show up
Another question:
How can I add MyText to the ASP.NET page like the following?
<asp:Button Text="<%=MyText%>" .../>
I dont want to edit the code like btn.Text=MyText, just want to do that on the .aspx file as required.
Answers to second question. (Btw, you should only ask one question at a time here on Stack Overflow.)
You could use
<asp:Button Text="<%# MyText %>" />
if you call DataBind() in you code behind.
public void Page_Init(object sender, EventArgs e)
{
DataBind();
}
If the databind is expensive, I believe you could use this code, so that you only call it once, and then save the values in the ViewState.
public void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataBind();
}
}

Resources