I'm having a bit of difficulty setting a variable from the code behind and utilising it in the ASP.NET page (setting it as the value inside a textbox). My webpage simply errors and says it does not exist in the current context. The variable is declared and set all in the Page_Load method.
Here is the relevant ASP.NET code. I assume you will not need to see the code behind, as I have tested outputting the variable via the codebehind (using Response.Write) and that works fine.
<asp:TemplateField HeaderText="Initial Path"
SortExpression="Initial_Path">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server"
Text='<%# initialPath %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox8" runat="server"
Text='<%# initialPath %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# initialPath %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Thanks :)
EDIT: Ok sure, here's the relevant parts of the code behind
string schedID = sched.SchedulerInstanceId;
JobDetail jobDetail2 = sched.GetJobDetail(Request.QueryString["JOB_NAME"], "sched1");
JobDataMap dataMap2 = jobDetail2.JobDataMap;
initialPath = dataMap2.GetString("initialPath");
Response.Write(initialPath);
The response.write is for debugging - it outputs the variable correctly so the variable is actually set
EDIT 2: Here is the code behind
public partial class EditJobDetails : System.Web.UI.Page
{
public string initialPath { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
//Scheduler stuff for Quartz.NET, removed because of length
string schedID = sched.SchedulerInstanceId;
JobDetail jobDetail2 = sched.GetJobDetail(Request.QueryString["JOB_NAME"], "sched1");
JobDataMap dataMap2 = jobDetail2.JobDataMap;
initialPath = dataMap2.GetString("initialPath");
}
What's not working is just the <%= initialpath %> in the ASP form. It simply just doesn't show anything, like the variable is assigned nothing
In the codebehind you need to create it as a public property. Then you can use it all you want in the aspx page.
in code behind:
public string yourvar { get; set; }
in aspx:
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server"
Text='<%= yourvar %>'></asp:TextBox>
</EditItemTemplate>
What I don't see here is where the call to DataBind() is made. If it's made before is set then <%#initialPath%> will write its starting value (null, which gets written as an empty string in such a case). If it's not made at all, then <%#initialPath%> will never write anything. You need to make sure DataBind() is called at an appropriate time, such as at the end of Page_Load
<%# initialPath %> is a databinding expression meaning that ASP.NET will look for a initialPath property on the collection you are binding to. Obviously such property doesn't exist as this is a local variable.
Related
This is how i created my DropDownListLehrbeginn in my GridView. sss
<asp:TemplateField HeaderText="Lehrbeginn" SortExpression="lehrbeginn" HeaderStyle-Width="40px">
<EditItemTemplate>
<asp:DropDownList ID="DropDownListLehrbeginn" runat="server"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelLehrbeginn" runat="server" Text='<%# Bind("lehrbeginn") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
I want to add ListItems with c# like this:
DropDownListLehrbeginn.Items.Add(new ListItem(DateTime.Now.Year.ToString()));
DropDownListLehrbeginn.Items.Add(new ListItem(DateTime.Now.AddYears(1).Year.ToString()));
DropDownListLehrbeginn.Items.Add(new ListItem(DateTime.Now.AddYears(2).Year.ToString()));
DropDownListLehrbeginn.Items[1].Selected = true;
It doesnt work unfortunately. How can i fix this issue? DropDownListLehrbeginn is not available in code behind
Your code looks fine. Because your drop down list is within another element you probably just need to use something like this
DropDownList ddlList = (DropDownList)NameOfGridView.FindControl("DropDownListLehrbeginn");
I'm trying to make this code work:
<asp:CheckBox ID="statusChk" runat="server" Visible="true" Enabled="false" Checked='<%# status("de_cancel") %>'></asp:CheckBox>
What i'm trying to do is to retrieve the answer from the status function (which returns bool when i give a string) that i created in the c# source.
Doesn't give me compile error, but doesn't work. Edit: And btw, this is inside in a GridView
This works:
<asp:Label ID="lblInfo" runat="server" Visible="true" Text='<%# Bind("de_cancel") %>'></asp:Label>
But this is NOT what I'm looking for.
Sorry about my bad English.
I'm assuming that this is in a grid view, or some other repeater.
In that case, try this:
<asp:Label ID="lblInfo" runat="server" Visible="true" Text='<%# status(Eval("de_cancel")) %>'></asp:Label>
this is essentially calling your status() method and passing in the value of de_cancel
You may have to convert de_cancel inside your status method though as Eval returns an object.
Just do this in the codebehind:
protected void Page_Load(object sender, EventArgs e)
{
//Get your string variable and use it as input here
statusChk.Checked = status("de_cancel");
}
protected bool status(string strStatus)
{
if (strStatus == "de_cancel")
{
return true;
}
else
{
return false;
}
}
This will occur when the page loads. There are other event handlers you may want to use - see http://msdn.microsoft.com/en-us/library/6w2tb12s(v=vs.90).aspx
Here is an example of how an event handler (OnSelectedIndexChanged) can be used to set a checkbox when a drop-down value is changed. As the accepted answer points out, you will need to set the AutoPostBack="true" option on the control if you want the event handler method to fire (and the page to refresh) when a control is changed:
Getting a dropdownlist to check a checkbox in asp.net/C#
Also, just looking at your code it looks like you might be using databinding, e.g. with a Repeater or ListView. In this case, please the examples for the ItemDataBound event handler below:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemdatabound(v=vs.110).aspx
http://www.codeguru.com/csharp/.net/net_asp/tutorials/article.php/c12065/ASPNET-Tip-Use-the-ItemDataBound-Event-of-a-Repeater.htm
Thanks to Darren tip, i was able to do it. Here what i did to make it work:
<asp:CheckBox ID="statusChk" runat="server" Visible="true" Enabled="false" Checked='<%#status(Convert.ToString(Eval("de_cancel")))%>'></asp:CheckBox>
Thanks everyone. ;)
I have a formview, connected to an objectDataSource.
It's a pretty easy code in the "EditItemTemplate" of the formview, implemented in the .aspx class:
<EditItemTemplate>
Var1: <asp:TextBox ID="txtVar1" runat="server" Text='<%# Bind("var1") %>' />
Var2: <asp:TextBox ID="txtVar2" runat="server" Text='<%# Bind("var2") %>' />
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="ButtonSave_Click" />
</EditItemTemplate>
When pressing "save" the data of "var1" and "var2" passes with the object to the next update method, that is implemented in the business logic class:
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
public void UpdateMyObject(MyObject updatedObject)
{
if (updatedObject.EntityState == EntityState.Detached)
mContext.MyObjects.Attach(updatedObject);
mContext.ObjectStateManager.ChangeObjectState(updatedObject, System.Data.EntityState.Modified);
int numberOfAffectedRows = mContext.SaveChanges();
if (numberOfAffectedRows == 0)
throw new DataNotUpdatedException("No object updated!");
}
The thing is that I need to pass a 3rd variable from the aspx to the business logic, without exposing it to the user through a textbox. It's a Guid and there is no need to change it.
With the current code, the Guid that is assigned to the "updatedObject" is only zeros and not the original Guid of the object, that it has and would have passed if I had binded it to a textbox, using a <%# Bind("entryGuid") %>.
So, my question is how can I pass this Guid (or any other variable) to the business logic layer, without the need to expose it? Is there an elegant way to bind data to the updated object not through a textbox?
Thanks!
Since #tzerb's answer left out an example, I thought I'd provide one.
<EditItemTemplate>
Var1: <asp:TextBox ID="txtVar1" runat="server" Text='<%# Bind("var1") %>' />
Var2: <asp:TextBox ID="txtVar2" runat="server" Text='<%# Bind("var2") %>' />
Var3: <asp:HiddenField id="hdnVar3" runat="server" Value='<%# Bind("var3") %>' />
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="ButtonSave_Click" />
</EditItemTemplate>
I think you're looking for
input type="hidden"
This will hide the input from the users and will be available in the post back. The users will be able to see it if they 'view code'.
Here is an example. The problem is that the Label2 has always Visible=true, regardless of the if-else result. How can i know if its visible or not?
<asp:GridView runat="server" ID="gdv">
<asp:TemplateField>
<ItemTemplate>
<%if (!IsItem)
{%>
<asp:TextBox runat="server" Text='<%# Eval("Qtde") %>'></asp:TextBox>
<%}
else
{ %>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Qtde") %>'></asp:Label>
<%} %>
</EditItemTemplate>
</asp:TemplateField>
I cannot access IsItem, since the class where i want to check the label for visibility is not the page code-behind. Also these controls are in a gridview.
The class method that checks for visibility is not called directly by the page, its a private method of a server control that extends a grid view and is supposed to work with a varierty of cases.
Thanks.
EDIT------------
I have found an alternative, decent solution. Still, i'm curious to know if it's possible to do the aforementioned. Thanks you for your time,
You are displaying that Label based on a variable called "IsItem." Why can't you use that to determine the Label's visibility?
simple question
in code behind(.cs) we have
string error="11000114S";
in ASP (.aspx) we have:
<asp:TextBox ID="text" runat="server" EnableViewState="false" AutoPostBack="false"/>
<asp:RequiredFieldValidator id="textValidator" runat="server" controlToValidate="text"
errormessage=(our string error="11000114S")>
So how to do this, assign value from cs-> saved error list to html ?
It is as simple as adding this to your .cs .. if I understand you correctly
string error="11000114S";
textValidator.ErrorMessage = error ; // or what ever you want
this should do
protected string error="11000114S";
<asp:TextBox ID="text" runat="server" EnableViewState="false" AutoPostBack="false"/>
<asp:RequiredFieldValidator id="textValidator" runat="server" controlToValidate="text"
errormessage='<%# error %>'>
You can assign error messages dynamically to your validators in your code behind. I.e.:
this.textValidator.ErrorMesssage = error;