not able to get value from aspx page to aspx.cs - c#

This is my javascript function where value comes from other page and it is received perfectly, but How can i retrieve "divConversation" value in cs page .
This is my code
function myLoad() {
document.getElementById('divConversation').innerText = getParameterByName("id");
}

The Main Problem With Your Requirement is that the server side code is executed first, So what you need is to receive the passed value on page_load instead of receiving on aspx page.
And this can be done by
String passedValue=Request.QueryString["id"] as string;

This is a really simple demonstration:
aspx/markup; this will set the value of your hidden field as you type
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
<script>
document.getElementById('<%= TextBox1.UniqueID %>').onkeyup = function (evt) {
document.getElementById('<%= HiddenField1.UniqueID %>').value = document.getElementById('<%= TextBox1.UniqueID %>').value;
}
</script>
Code behind (.cs)
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(HiddenField1.Value);
}

Related

Clear Specific Textboxes ASPX page

I'm having trouble trying to clear these banking and routing numbers that are in a textbox on an aspx page. I've seen it used where they would just specify the ID of the textbox and do a textbox.text = String.Empty(). But that doesn't seem to work here. Maybe I'm using the wrong ID?? I also tried using JQuery .val("") but that didn't seem to work either.
Here's the code, i'd like to clear both Routing and Account text fields on click of a button:
<div id="DivUser1BankInfo" class="labelAndTextboxContainer">
<div class="labelContainer">
<asp:Label CssClass="rightFloat" ID="User1LabelRoutingNumber" runat="server" Text="Routing #:"></asp:Label><br />
</div>
<div class="textboxContainer">
<asp:TextBox ID="User1TextRoutingNumber" CssClass="leftFloat " runat="server" Font-Size="Smaller" Width="180px"
Text='<%# Bind("User1BankRoutingNumber") %>'
Visible='<%# ApexRemington.BLL.VendorBLL.ShowUser1BankInfo((string)Eval("User1BankInfoEditUser")) %>' /><br />
</div>
<div class="labelContainer">
<asp:Label CssClass="rightFloat" ID="User1LabelAccountNumber" runat="server" Text="Account #:"></asp:Label><br />
</div>
<div class="textboxContainer">
<asp:TextBox ID="User1TextAccountNumber" CssClass="leftFloat " runat="server" Font-Size="Smaller" Width="180px"
Text='<%# Bind("User1BankAccountNumber") %>'
Visible='<%# ApexRemington.BLL.VendorBLL.ShowUser1BankInfo((string)Eval("User1BankInfoEditUser")) %>' /><br />
</div>
<button type="button" id="clearButton1">Clear</button>
<div class="button">
<asp:Button ID="User1ClearBankInfo" runat="server" Text="Reset"
Visible='<%# ApexRemington.BLL.VendorBLL.ShowUser1BankInfo((string)Eval("User1BankInfoEditUser")) %>' OnClick="clearFields_btn"/><br />
</div>
The OnClick= "clearFields_btn" code behind =
protected void clearFields_btn(object sender, EventArgs e)
{
}
Thanks for any help!
I haven't worked with ASP.NET in a little while, but I think you may want the OnClientClick event, not OnClick. OnClientClick is for client-side code (your jQuery/JavaScript) and OnClick is for server-side code (your C# or VB.NET).
You'd also want your OnClientClick event method to return false, or the server-side code will also fire.
So I think you want something like:
<asp:Button ID="User1ClearBankInfo" runat="server" Text="Reset"
Visible='<%# ApexRemington.BLL.VendorBLL.ShowUser1BankInfo((string)Eval("User1BankInfoEditUser")) %>
OnClientClick="clearText();"/>
And then clearText would look like this:
<script>
function clearText()
{
//our two IDs
$('input[id*="User1TextRoutingNumber"]').each(function(index) {
$(this).val('');
});
$('input[id*="User1TextAccountNumber"]').each(function(index) {
$(this).val('');
});
return false;
}
</script>
EDIT: shoot, I see my mistake. Fixed the code to clear the text of the textbox, not the button ("this").
Edit: removed the space from the "clear" text val.
EDIT: Made search a little more flexible, less dependent on GridView or no GridView.
Try this
<script>
var clear = function(textboxID){$('input[id*=' + textboxID + ']').val('');};
return false;
</script>
<button id="btClearText" onclick="javascript:return clear('txtName');">
but if you need a more specific answer then please post more information
You need something like this. Assuming you want a client side solution (not very clear from your question).
<script type="text/javascript">
function clearTextBox() {
document.getElementById("<%= User1TextRoutingNumber.ClientID %>").value = "";
//or
$("#<%= User1TextRoutingNumber.ClientID %>").val("");
}
</script>
The <%= User1TextRoutingNumber.ClientID %> will ensure you get the correct ID for javascript/jQuery.
A server side solution would be:
protected void clearFields_btn(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
TextBox tb = GridView1.Rows[i].FindControl("User1TextAccountNumber") as TextBox;
tb.Text = "";
}
}

Calling asp net button click event with JS will not call server side event

Sorry to post perhaps a silly problem here, but I'm at my wits end with it. I have a hidden field with a button inside an update panel like so:
<asp:UpdateProgress runat="server" ID="updprCompLines" AssociatedUpdatePanelID="updpanCompLines">
<ProgressTemplate>
<img src="../Images/ajax-loader.gif" alt="Please wait..." /> </ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel runat="server" ID="updpanCompLines" UpdateMode="Conditional">
<%--<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnFillMembers" />
</Triggers>--%>
<ContentTemplate>
<div>
<asp:HiddenField ID="hdnField" runat="server" />
<asp:Button ID="btnFillMembers" runat="server" style="display:none;"
Text="DummyButton" onclick="btnFillMembers_Click" />
</div>
The update panel also contains a gridview and inside my gridview I have a link button:
<ItemTemplate>
<asp:LinkButton ID="lkbtBenefName" runat="server" Text='<%#Eval("COMPETENCE_CODE") %>'
OnClientClick='<%#Eval("COMPETENCE_LINE_ID", "return SelectedCompetence({0})") %>'/>
</ItemTemplate>
The call is to a JS function that is supposed to call the above button:
<ajaxToolkit:ToolkitScriptManager runat="Server" EnablePartialRendering="true" ID="ScriptManager1" EnablePageMethods="true"/>
<script type="text/javascript">
function SelectedCompetence(CompetenceLineId) {
document.getElementById('<%= hdnField.ClientID %>').value = CompetenceLineId;
var clickButton = document.getElementById('<%= btnFillMembers.ClientID %>');
clickButton.click();
}
</script>
Button click event method:
protected void btnFillMembers_Click(object sender, EventArgs e)
{
lblGvMemError.Text = "";
lblGvMemError.ForeColor = Color.Red;
if (hdnField.Value != null || hdnField.Value.ToString() != "")
{
try
{
int CompLineId = Convert.ToInt32(hdnField.Value);
GetSelectedCompLineMembers(CompLineId);
}
catch (Exception ex)
{
lblGvMemError.Text = "Error: " + ex.Message;
}
}
updpanCompLinesMembers.Update();
}
The problem is that while debugging, it never runs the click event and it doesn't give any error messages either. I don't understand, I have a similar form where this works; I don't get why it doesn't here... Any help please?
Have you confirmed that SelectedCompetence is being called via an alert or similar? Additionally, have you made sure that the clickButton variable is being assigned to successfully?
I know this isn't an answer, but don't yet have the reputation to comment, and sometimes it's the easy stuff so maybe this will help! :)

Reading value of hiddenfield after javascript on server side

I have a problem with asp:hiddenfield, when I change its value in client side and wants to get it in server side , it gives me null...
here is client side code :
function pageLoad() {
var gV = $('#<%=HiddenField1.ClientID %>');
gV.val("1");
}
and I want to get the value of hiddenfield in server side code :
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Text = HiddenField1.Value;
}
but the result for text of button is null... why??
thanks in advance:)
After this line:
var gV = $('#<%=HiddenField1.ClientID %>').val();
gV is a string, so gV.val("1") doesn't make sense.
Try this:
var gV = $('#<%=HiddenField1.ClientID %>');
gV.val("1");
Now, that shouldn't cause HiddenField1.Value to be null... did you mean empty?
Could you try with document ready?
<asp:HiddenField runat="server" ID="HiddenField1" />
<script type="text/javascript"
src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
var gV = $('#<%= HiddenField1.ClientID %>');
gV.val("1");
});
</script>
<asp:Button runat="server" ID="Button1" OnClick="Button1_Click" />
Or - Use ScriptManager to call pageLoad
<asp:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager>
<asp:HiddenField runat="server" ID="HiddenField1" />
<script type="text/javascript"
src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function pageLoad() {
var gV = $('#<%= HiddenField1.ClientID %>');
gV.val("1");
}
</script>
<asp:Button runat="server" ID="Button1" OnClick="Button1_Click" />

How to call a javascript in C# and get a return value?

<script language="javascript" type="text/javascript">
function myjavascriptfn()
{
//debugger;
var strValue= "test";
return strValue
}
How do I call this javascript function in my code behind and proceed appropriately with respective of return values.
You can easily declare JavaScript to be run on the Client using
ScriptManager.RegisterStartupScript(this, this.GetType(), "launchpage", "
function javascriptfn() {
var strValue= 'test';
return strValue;
}
document.getElementById('"+HiddenField1.ClientID+"').value = javascriptfn();
document.getElementById('"+saveProgressButton.ClientID+"').click();
", true);
note: I have divided out the JavaScript out onto multiple lines to make it easier to read but it should all be on one line.
Your problem comes with the second part of the question, sending the data back, you will most likely need a postback (partial or full or handle it with AJAX.
I would add an updatepanel with a asp hiddenfield and a hidden button to trigger it, populate the value of the hidden field with whatever this function is for had have some code in your code behind to capture the event.
<asp:UpdatePanel ID="responcetable" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:Button ID="saveProgressButton" runat="server" Text="Button" CssClass="displaynone" />
</ContentTemplate>
<Triggers><asp:AsyncPostBackTrigger ControlID="saveProgressButton" EventName="theeventtodealwiththis" /></Triggers>
</asp:UpdatePanel>
and on the serverside
protected void theeventtodealwiththis(object sender, EventArgs e)
{
// some logic to handle the value returned
}

updating variable from within update panel

im trying to update a variable from within an update panel:
<script type="text/javascript">
var v = 1;
</script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="btnDone" runat="server" Text="Done" onclick="btnDone_Click" />
<asp:Literal ID="litnew" runat="server"></asp:Literal>
</ContentTemplate>
</asp:UpdatePanel>
<script type="text/javascript">
function updateint() {
alert(v);
}
</script>
<input type="button" onclick="updateint()" />
code behind
protected void btnDone_Click(object sender, EventArgs e)
{
string kiss = LipImageCreator.createImage(); //this returns a file path
litnewlipsurl.Text = "<script> v = '" + kiss + "'; </script>");
}
if i click the button run the updateint() function before i hit the btnDone button i get the alert saying '1' as expected. after i click the btnDone button the javascript is written to the literal as expected but when i click the updateint() button again i still get '1' and not the filepath i was expecting....
You must use ClientScript.RegisterStartupScript() to get the ajax handler to run your script when the postback completes.

Categories