I have three RadListBox controls on a page. When a user drags an item from the first listbox to the second listbox, I need the third one to automatically generate a textbox.
I've figured out how to add a regular HTML textbox with JavaScript. However, I'm not able to access this in the code behind page because adding a runat=server field causes an error. Here's the client JavaScript code I'm using to insert a textbox:
//Add textbox to 3rd listbox
var listbox = $find("<%= ListBoxThree.ClientID %>");
listbox.trackChanges();
var textboxItem = new Telerik.Web.UI.RadListBoxItem();
textboxItem.set_text(item.get_text()); //sets the ID
textboxItem.set_clientTemplate(" <input id=\"#= Text #\" type=\"text\" /> ");
textboxItem.bindTemplate();
textboxItem.set_value
listbox.get_items().add(textboxItem);
listbox.commitChanges();
If I try adding runat=server I get the following error:
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Line 70: textboxItem.set_clientTemplate(" <input id=\"#= Text #\" type=\"text\" runat=\"server\" /> ");
Parser Error Message: The server tag is not well formed.
When the user hits the Save button though, the item's Text and Value fields do not get updated with the text they've entered.
protected void SaveButton_Click(object sender, EventArgs e)
{
foreach (RadListBoxItem item in ListBoxThree.Items)
{
string value = item.Value;
string text = item.Text;
}
}
If someone could point me in the right direction I'd really appreciate it!
Related
I have a WebForm ASP label and button. I am setting the label's value on page load. For example, the label text on page load is 2 items selected. This comes from the database. Then if the user changes the selection then it counts the selected values by jQuery and sets the text as 5 items selected.
When I click on the submit button to save changes, again it resets to 2 items selected. I didn't use an update panel. I don't know what is going on here. Can anyone please explain this scenario?
$("#lblCount").text($('#grdProducts').find('input#chkSelect:checked').length + ' Complementary Products added');
C# on page load:
lblCount.Text = ComplementaryproductCount.ToString() + " Complementary Products added";
I do not understand why the label text is changed on button click. I couldn't find anything while debugging too.
Thanks
When you set lblCount.Text in your code, that value is set into the ViewState of the page... that means when your page is posted back to the server (to handle an event, etc) ASP.Net knows what lblCount.Text was originally and can re-render the HTML with the same value.
As part of that post-back to the server, the browser will send back that ViewState along with any input control values (things like textboxes, dropdowns, hidden field).
What it does NOT do is post-back any changes you might have made to the elements on the page via things like jQuery (other than input controls I mentioned above).
The result is that although you've changed the element on the screen, the server knows absolutely nothing about that change, and it will re-send the original HTML for the label back to the browser.
Your only option is to do something as suggested by #John in his comment... you need to store the fact the element has changed in an input, and then use that.
For instance...
<asp:Label runat="server" id="lblCount" />
<asp:HiddenField runat="server" id="hdnCount" />
function updateCount(newCount) {
$("#<%=lblCount.ClientID%>").text("Count: " + newCount.toString());
$("#<%=hdnCount.ClientID%>").val(newCount.toString());
}
Then in your code-behind you can have...
if (!Page.IsPostBack)
{
var count = 1;
lblCount.Text = String.Format("Count: {0}", count);
hdnCount.Value = count.ToString();
}
else
{
lblCount.Text = String.Format("Count: {0}", hdnCount.Value);
}
I have an asp.net web form which on one page has a multi line textbox. When the user types in this field and continues the details are displayed on a confirmation page and displays as the user entered
Example
This is the first line
This is the second line
But when I click my edit button (which directs me back to my page) my textbox displays as
This is the first line<br /><br />This is the second line
I want it to keep its styling but don't know how to do this. The details are stored in the session.
Code behind
protected void Step07SubmitButton_Click(object sender, EventArgs e)
{
Session["Step07OtherDetailsField"] = Step07OtherDetailsField.Text.Replace("\r\n", "<br />");
Response.Redirect("/Quotation/pg3.aspx");
}
I tried the following in my Page_Load
Step07OtherDetailsField.Text.Replace("<br />", "\r\n");
and also
Step07OtherDetailsField.Text.Replace("<br />", Environment.NewLine);
But for some reason when I debug it, it says that my .text is empty but the previous wording is actually still displayed in the field and the <br /> is also still displayed.
You don't need to replace anything. You just store value in session and display it to any textbox, it will maintain \r and \t.
If you want to display result in span then display result to textbox with multiline then apply css for no border,etc.
I'm going crazy trying to send the value of an HTML text input (without runat="server") from a user control to a code behind. I get an empty string with no error. The steps are as follows:
User clicks on the ImageButton with ID="name_btn".
The value must be passed to the code behind. I'm testing to see if the value is passed by using the page load (if (IsPostBack)) and the button click function but still NO VALUE in both.
Note that for some other reason, I don't want the text input to run on the server so I don't want to add runat="server". Any help please?
User Control page (name.ascx)
<input type="text" ID="name" />
<asp:ImageButton runat="server" ID="name_btn" OnClick="name_Click" ImageUrl="~/icon-ok.png" />
User control - Code behind (name.ascx.cs)
public string nameBox;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
nameBox = Request.Form["name"];
Response.Write("Name: " + nameBox);
}
}
protected void name_Click(object sender, EventArgs e)
{
nameBox = Request.Form["name"];
Response.Write("Name: " + nameBox);
}
Master Page
<uc:name runat="server" ID="UCname" />
I would imagine that there is a bit of a conflict with what you are trying to do. For example, on the server you are using Request.Form[""] to get the html input element but you are requesting it by the id attribute. Request.Form supports finding elements by index and name.
So have you tried adding a name="" to the html input element?
you should add a name attribute to your input tag, example:
<input type="text" id="name" name="name"/>
then you can use:
if (IsPostBack)
{
nameBox = Request.Form["name"];
Response.Write("Name: " + nameBox);
}
The Request.Form(element)[(index)|.Count] Parameters:
element
The name of the form element from which the collection is to retrieve values.
index
An optional parameter that enables you to access one of multiple values for a parameter. It can be any integer in the range 1 to Request.Form(parameter).Count.
You can pass the value to codebehind using a javascript in two ways.
1. Onclick of your image button call a javascript.
Set your textbox value in __EVENTARGUMENT and call __doPostback(). And in pageload you can check the value in __EVENTARGUMENT.
From the javascript you can directly call a Function in your server using PageMethods. Here your function must be a WebMethod.
I have <td id="StatusPreview" runat="server"></td> and it gets populated by a js function by:
document.getElementById('StatusPreview').innerHTML = Ext.getCmp('TYComboEdit').getRawValue();
Now, I would like to change the content of the td in c# when a button is clicked.
I created the following method:
protected void hiddentoggletofrenchBackend(object sender, DirectEventArgs e)
{
this.StatusPreview.InnerHtml = "aaaaa";
}
It does not change the content on the td. However, if I place an alert after setting the InnerHtml it alerts aaaaa even though the td content has not changed to reflect this. If I place an alert before setting the InnerHtml the alert is blank.
How can I change the InnerHtml of the div?
Thank you!
UPDATE:
If I change the html to <td id="StatusPreview" runat="server">q</td> the alert shows q if it is placed before setting InnerHtml, and switches to aaaaa if placed after.
It is as if InnerHtml is taking the value on pageload, not the current value.
To update an ASP.NET control during a DirectEvent, you should call the .Update() method.
protected void hiddentoggletofrenchBackend(object sender, DirectEventArgs e)
{
this.StatusPreview.InnerHtml = "aaaaa";
this.StatusPreview.Update();
}
Adding runat=server to a td element turns it into a HtmlTableCell control. The relevant property to set the inner text on this control is InnerText.
As this is a server side control, any change is only going to happen after postback to the server. That would mean the entire page is reloaded and re-rendered. You can examine requests to the server and the server responses with the free tool Fiddler. Assuming a postback is actually happening, are you sure you're not overwriting the new inner text with JavaScript which runs on page load?
Do you even need to do a postback for this? If "aaaaa" is not a placeholder for what will become a database or some other lookup, I would render the alternate text into a hidden div or into some JavaScript and do the text change entirely in JavaScript.
I have the following problem.
I have a asp:textbox on the page, runat server with an id of say txt
This text box is in a <div>, nothing special. ie:
<div>
<asp:TextBox id="txt" runat="server"></asp:TextBox>
</div>
The problem is there is some java script which when you push the corresponding button it doubles (copies) the div. This is by design. It is meant to.
When you hit save but at the bottom of the page on a asp:Button, it can't find the value I need because it returns two results.
In the code behind:
(Textbox) blah = (Textbox)senderbutton.FindControl("txt");
string test = blah.text
But the result is essentially--> "The value in the textbox , The value in the textbox"
I.e. it is there twice. I have worked around this by doing the following:
string[] test = blah.text.split(new[] { ',' })
and then only calling the second value in the array or whatever.
BUT, now I have this situation but the problem is that a user can enter a string with a ' , ' in it, hence the splitting goes to crap....
So can I find a control with an id, but only find the nth occurence of it in the code behind?
Seems you need to give different name(like txt0,txt1...) for each copy of the input controls.
You can do this using javascript up on client click(prior to form submission) of your asp button
-- Javascript method
function ModifyName() {
var x = 0;
$("input[name='txt']").each(function () {
$(this).attr("name", $(this).attr("name") + x);
x++;
});
}
-- asp:Button
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
OnClientClick="ModifyName();" onclick="btnSubmit_Click" />
So in code behind you can get the values like this...
protected void btnSubmit_Click(object sender, EventArgs e)
{
var resultArr = Request.Form.AllKeys.Where(x => x.Contains("txt"))
.Select(x => Request.Form[x]).ToArray();
}
Not a very nice solution, but you could examine the Request.Form collection directly upon postback and write some code to process your dynamically added textbox fields.
The best solution would be: avoiding copying the div in js. Since you said "This is by design. It is meant to."(even I really doubt it), there are some alternative solutions:
(1) Don't use the default submit behavior of the form. That is, in the click (js) event of the save button, organize the data in the form and then submit it.
(2) Modify the second(copied) textbox's id so that its id is different from the original one, and then get the data in code behind.
I am not sure why you using FindControl method to find the control when you can directly access the txt control from code behind.
You can get results easily
String test = txt.Text;