There is a complex function written in jquery that runs if the href below is clicked by the user. Is there any way I can simulate this click in the code behind of the asp form? In other words, I need to "click" the href called "shipping_question_ID_product_received_as_ordered_link" from my code behind to prepopulate this input. Can anyone assist?
<div class="no"><label class="label-1" for="shipping_question_ID_product_received_as_ordered_not_acceptable">Not Acceptable</label></div>
<div class="yes"><label class="label-2" for="shipping_question_ID_product_received_as_ordered_acceptable">Acceptable</label></div>
<label class="universal-label"></label>
<input type="radio" id="shipping_question_ID_product_received_as_ordered_not_acceptable" name="shipping-question-ID-product-received-as-ordered" value="Not Acceptable" runat="server">
<input type="radio" id="shipping_question_ID_product_received_as_ordered_acceptable" name="shipping-question-ID-product-received-as-ordered" value="Acceptable" checked="true" runat="server">
#ArindamNayak had the right idea, but there may be issues with his implementation. If you add the below script to your code-behind, you will achieve what I believe is your desired result:
var autoRunScript = string.Format("<script>$(function() {{ $('#{0}').click(); }} );</script>", shipping_question_ID_product_received_as_ordered_link.ClientID);
Page.RegisterClientScriptBlock("keyClientBlock", autoRunScript);
The difference here is that without wrapping your $(link).click() in a ready handler, jQuery may not process the command correctly. Additionally, you have to be careful with the ID that you use as it could be modified to include containers.
You can use following.
string jScript;
jScript="<script>$('#linkid').click()</script>";
Page.RegisterClientScriptBlock("keyClientBlock",jScript);
So this will add a JS code block programatically to aspx and this JS will execute to click the a href ( having id = "linkid"
Related
I have problem in sending some value from code behind to another page.
I have an input button that's enabled when the check box is true (write with js) now I want a call event for this button so the code behind that will send some value to another page .
This is my html button:
<input type="button" Class="btn btn-primary pull-left" id="del_event" name="del_event" value="confirm" disabled="disabled">
I have 2 values that need be send in the code behind.
How can I send these values to another page?
If you use just an HTML tag instead of an asp.net button you may use query string. Wrap your button inside an <a> element. Like this:
<input type="button" value="click">
you can use session to keep the value in this page behind ,and then get it in anohter page behind.
I am using ASP.NET for a web page in order to make some server calls that involve looking up user organization information. Based on that information, we need to either hide or display a div. In the header I have a C# function that definitely runs. I have tried the following lines to hide the div.
divID.Style.Add("display","none");
and
divID.Visible = false;
In the body, I am currently using an asp:Panel that runs at server and contains the id "divID". No matter what I do, I can't get the div to hide (without manually putting the styling in). I tried putting the scripts before and after the body, and it didn't make a difference. Any suggestions on the best way to do this would be appreciated.
EDIT:
Here is the C# initiating code.
<script runat="server" language="C#">
void getUserInfo(Object sender, EventArgs ev){
The rest of the C# code is irrelevant, but the relevant line shown above is definitely being run.
The HTML portion looks something like this.
<asp:Panel runat="server" id="divID" style="width:200px; height:130px; ">
<div style="text-align:center">Test Data</div>
</asp:Panel>
C# code is always compiled and run from the server-side, and so cannot impact the state of a page once rendered unless you use postbacks or callbacks. If you want to change the visible state of a control on the client-side, you will need to use Javascript on the client side (possibly triggered by a button click) to show and hide the control.
As an example, check out the solution at the link below.
https://forums.asp.net/t/1603211.aspx?Show+hide+div+on+button+click+without+postback
<script type="text/javascript">
function ToggleDiv(Flag) {
if (Flag == "first") {
document.getElementById('dvFirstDiv').style.display = 'block';
document.getElementById('dvSecondDiv').style.display = 'none';
}
else {
document.getElementById('dvFirstDiv').style.display = 'none';
document.getElementById('dvSecondDiv').style.display = 'block';
}
}
</script>
<asp:Button ID="btn" runat="server" Text="Show First Div"
OnClientClick="ToggleDiv('first');return false;" />
<asp:Button ID="Button1" runat="server" Text="Show Second Div"
OnClientClick="ToggleDiv('second');return false;" />
<br />
<div id="dvFirstDiv" style="display: none;">
First Div
</div>
<div id="dvSecondDiv" style="display: none;">
Second Div
</div>
In the header I have a C# function that definitely runs.
If you're talking about the HTML page header - no, it definitely not running. C# code is executed only server side.
Based on your post, I'm assuming we're talking WebForms here and yo have a script block in your aspx file. While this is fine, I recommend placing the server-side code into a code behind file.
So all you need to do is to add a handler for the PreRender phase of the page life cycle and place your logic for showing/hiding the div in there.
public void Page_Prerender(object sender, EventArgs e)
{
divID.Visible = false;
' OR
'divID.Style.Add("display","none");
}
Note that setting the Visible property of a WebForms control excludes the control from rendering to the page, whilst setting display: none renders it as HTML but it isn't displayed on the page.
Try in backcode: divID.Controls.clear();
This worked for me.
I'm creating a datetime picker using code
<div class='input-group date'>
<input type='text' class="form-control" id='datetimepicker'/>
</div>
<script type="text/javascript">
$(function () {
$('#datetimepicker').datetimepicker({
viewMode: 'years'
});
});
</script>
which allows me to create a datetime input. For the backend code, I am able to add fields to the database for asp:textbok's, asp:labels etc, but not for this input.
Here is an example of others I am adding
cmd.Parameters.AddWithValue("#jobList", jobList.Text);
cmd.Parameters.AddWithValue("#jobCriteria", jobCriterta.Text);
When I try to do this for datetime, the option for the ID=datetimepicker does not appear and I'm not able to add whatever datetime is in that input to the database.
Cheers in advance for any help I'm sure it's simple!
If you want to access an html control in the back end you need to give the attribute runat="server" along with the tag definition.
<input type='text' runat="server" class="form-control" id='datetimepicker'/>
Then change your script as like this:
$("#<%=datetimepicker.ClientID%>").datetimepicker({
viewMode: 'years'
});
After adding this to the tag you will get datetimepicker in the back end. and you can access it's value to do the rest of operations. and one more suggestion use .Parameters.Add() instead for Parameters.AddWithValue()
Replace the input tag with TextBox server control.
<asp:TextBox ID="datetimepicker" runat="server" ClientIDMode="Static" />
Remember to set ClientIDMode as "Static" to make sure jQuery code can find the textbox correctly. Then the backend C# code can also use this textbox as web control object.
I am populating a ListView with HTML from a database using a Literal with Text='<%#Eval("HTMLData")'%>. When I trigger a PostBack, changes to the loaded HTML are not being reflected in litRowData.Text.
ViewState is enabled for the page, the ListView, and the Literal in the ItemTemplate, and I am making sure to only populate the ListView with initial values from the database when if(!IsPostBack) is true in Page_Load.
<asp:ListView ID="lvForm" runat="server"
DataKeyNames="RowID" ItemPlaceholderID="phRow"
EnableViewState="true">
<LayoutTemplate>
<asp:PlaceHolder ID="phRow" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<asp:Literal ID="litRowData" runat="server" Text='<%#Eval("HTMLData")%>'
EnableViewState="true"></asp:Literal>
</ItemTemplate>
</asp:ListView>
I need to be able to capture changes to the contents of the loaded HTML controls. Since this HTML comes from a database table, I can't just use ASP controls inside the ItemTemplate. Can anyone see something I'm missing, or suggest an alternative way to do this?
Edit:
To clarify a little more, I'm trying to load form input elements dynamically from a database, render them as HTML controls on the page, allow the user to modify their contents by entering text or selecting options, then capture the modified HTML and save it back to the database when the user clicks a save button.
The way postback works in .NET is actually a wrapper around the more basic idea of HTML forms. A basic example of HTML forms is:
<html>
<body>
<form action="" method="POST">
<input type="text" value="type here" />
<input type="submit" value="go" />
</form>
</body>
</html>
Roughly, what the .NET abstraction adds is:
<html>
<body>
<form action="" method="POST">
<input type="hidden" name="__VIEWSTATE" value="string-encoded-value" />
<input type="text" name="bob" value="type here" />
<input type="submit" value="go" />
</form>
</body>
</html>
Whereby on postback to your page, all input elements with names are mapped back into properties of your Page object, and the __VIEWSTATE hidden field is deserialized into all properties of objects that do not correspond to values of html input tags. For example, if Page.bob had a DateTime property associated with it, it would be stored in __VIEWSTATE possibly.
ASP.NET Literal tags in Page markup will get printed into the browser exactly as is, meaning that if you have <span>bob</span> as its value, that is how it will appear within the <form> tag. However, in plain HTML world, <form> tags when posted will only contain the values of certain form elements (aka not every div, span, p etc. gets posted back, only input, select, textarea and some others). So if your literal doesn't contain an input then it won't even get posted back meaning __VIEWSTATE will be used to restore the Value property of the Literal back to its initial state.
To fix this, you probably don't want to stick html into a Literal because even if you do it's not clear that it will get associated with the right property of your page. Instead, try a TextBox element or something else that gets written as an input element directly by the ASP.NET webforms code. Alternatively, try using javascript to allow modifications of flat text in divs if you don't need to persist the data.
This answer builds on the prior one now that you have a .NET TextBox control that is correctly posting back the value of edits. Right below it, you can add to code behind:
protected void Page_Load(object sender, EventArgs e)
{
litRowData.Attributes.Add("onKeyUp", "WriteText(this.value)");
}
Html:
<ItemTemplate>
<asp:TextBox ID="litRowData" runat="server" />
</ItemTemplate>
<div id="yourPreview"></div>
<script type="text/javascript">
function WriteText(val){
document.getElementById("yourPreview").innerHTML=val
}
</script>
I am currently using <a> tag to click and navaigate as mentioned in below code.I want to navigate the below code in c# tag in page_prerender event based on id selected.could any one help on this?
<li>Category 1</li>
<li>Category 2</li>
<div id="cat1">
Content goes here
</div>
<div id="cat2">
Content goes here
</div>
something like if url doesn't contain '#' then
Response.Redirect("www.yoururl.com/Default.aspx#Cat2");
is that what you're looking for?
clicking on the <a> in html is really the same as just adding #Cat2 to the end of the URL , the browser knows what to do from there
I believe you need a url rewriting layer for how you have it. The <a> links are simply going to be a GET request with no further information. And even then you would need the links to be href="cat1" or href="#cat1".
The simplest alternative would be using <asp:linkbutton /> which can have server-side onclick events.
You might also look into AJAX requests, or use javascript to hide and display the sections, if the content is dynamic based on the request AJAX would be required there.