asp.net textbox focus problems - c#

I am trying to set focus on textbox on page load in asp.net as follows
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
// fillUnitType();
//
fillLastCode();
txt_Grn_Date.Text = System.DateTime.Now.ToString("dd-MM-yyyy");
setinitialrow_lvl();
txt_Po_No.Focus();
}
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
}
But the textbox is not getting focused. What is that I am missing. I have used update panel is it because of that.? Or my css is slightly faulty.

Write following function in your codebehind and for every control call this function
private void Set_Focus(string controlname)
{
string strScript;
strScript = "<script language=javascript> document.all('" + controlname + "').focus() </script>";
RegisterStartupScript("focus", strScript);
}
Set
tapindex = 0
TextBox1.Focus();
or
textBox1.Select();
or
protected override void OnShown(EventArgs e)
{
textBox1.Focus();
base.OnShown(e);
}
or
setTimeout("myFocusFunction()", 500);
function myFocusFunction(){
$("#myTextBoxID").focus();
}

try this in javascript
<script language=javascript>
function fnLoad(){
document.getElementById("<%= txt_Po_No.ClientID %>").focus();
}
</script>
call "fnLoad()" function on "onLoad" event of body..
You need to add this function in body tag : Like
<body onload="fnLoad()">........</body>
Update:
try another way
<script language=javascript>
$(document).ready(function(){ document.getElementById("<%= txt_Po_No.ClientID %>").focus();})
</script>
or
<script language=javascript>
$(window).load(function(){ document.getElementById("<%= txt_Po_No.ClientID %>").focus();})
</script>

I have tried this with updatepanel and textbox inside it.
CodeBehind
.aspx
output

Try this code..
string jsCode= "<script language=javascript>document.getElementById('<%= TEXTBOX.ClientID%>').focus();</script>";
ClientScript.RegisterClientScriptBlock(GetType(), "txtbox",jsCode, false);

Related

how to show a label in an aspx page based on a request query string

i have to labels on page load. one comes from a query string and one is coded onto the page. as follows
<script runat="server" language="C#">
protected void Page_Load(object sender, EventArgs e)
{
lblMessage.Text = Request.QueryString["message"];
lblcard.Text = "Transaction made successfully!";
}
</script>
how do i check if lblmessage has a text then it should only display that label and hide the lblcard text,if lblmessage doesnt have a text then it should show the "transaction made " text
You would use the String.IsNullOrEmpty method documented here.
Your code would look something like this:
<script runat="server" language="C#">
protected void Page_Load(object sender, EventArgs e)
{
lblMessage.Text = Request.QueryString["message"];
if (String.IsNullOrEmpty(lblMessage.Text) {
lblcard.Text = "Transaction made successfully!";
}
}
</script>
if(!string.IsNullOrEmpty(lblMessage.Text))
{
lblcard.Text = "Transaction made successfully!";
}

Trying tocall a jquery function from server side

I am trying to call a jQuery function from the server using script manager but I'm not able to make the call. How should it be called? Any ideas would be appreciated.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDummyRow();
}
TW12HVGI();
}
void TW12HVGI()
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "dynamicForm();", true);
}
}
<script type="text/javascript">
// $(document).ready(function () {
$(function dynamicForm() {
$("#openForm").click(function () {
});
});
</script>
The issue is because you've defined dynamicForm() as the function you provide to the jQuery document.ready handler, so it's not in the correct scope. Try this instead:
function dynamicForm() {
$("#openForm").click(function () {
// do something...
});
}
Also note that you could make this a lot simpler by just calling dynamicForm() within your JS and removing the unnecessary RegisterStartupScript() from your server side logic.

Call jquery button click event from code behind

I am using the below code to display a div on button click event. Is there any way to call this button click event from C# code behind.
$(document).on("click", '#AddNew', function (e) {
e.stopPropagation();
if ($NewEntry.is(":hidden")) {
$NewEntry.slideDown("slow", "easeOutBounce");
$NewEntry.slideDown("slow", "easeOutBounce");
$filter.slideUp("slow");
return false;
} else {
$NewEntry.slideUp("slow");
return false;
}
});
use this code :
protected void myButton(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>myFunction(params...);</script>", false);
}
more read : Call jQuery function from ASP.NET code behind C#

Not able to call a javascript function through alert

I am creating an alert and trying to call a click event through javascript function when "OK" of alert is pressed.It runs pretty well if I create the alert on page_Load but When I crate the alert on clicking of a buttton, then on pressing "OK" of alert the required click event is not called.
This is how I create the alert
protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Startup", "Test();", true);
}
This is the javascript function which calls a click event
<script type="text/javascript">
function Test() {
alert('There is no Bookmarked Question Available');
document.getElementById('btnReview').click();
}
</script>
if btnReview is server button then try change you script like this
for asp.net
<script type="text/javascript">
function Test() {
alert('There is no Bookmarked Question Available');
document.getElementById('<%= btnReview.ClientID %>').click();
}
</script>
for asp.net mvc (razor)
<script type="text/javascript">
function Test() {
alert('There is no Bookmarked Question Available');
document.getElementById('#btnReview.ClientID').click();
}
</script>
You just add below code in javascript function
__doPostBack('btnSubmit','OnClick');
Try this
protected void Button1_Click(object sender, EventArgs e)
{
string scripts = #"
function Test() {
alert('There is no Bookmarked Question Available');
}";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Startup", scripts, true);
}

How to move to code in codebehind from javascript

I have a gridview .When i click on one row in it i have to go to javascript which is like this.
<script type="text/javascript" language="javascript">
function GetDetails(rowNo)
{
document.getElementById('hidRowNo').value = rowNo
document.getElementById('btnDet').click();
}
</script>
I have written following code in codebehind
protected void btnDet_Click(object sender, EventArgs e)
{
if (hidRowNo.Value != "")
{
int rowNo = Convert.ToInt32(hidRowNo.Value);
TextBox1.Text = GridView1.Rows[rowNo].Cells[0].Text;
TextBox2.Text = GridView1.Rows[rowNo].Cells[1].Text;
TextBox3.Text = GridView1.Rows[rowNo].Cells[2].Text;
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "GetDetails(" + e.Row.RowIndex + ");");
}
}
The problem is javascript is working but document.getElementById('btnDet').click(); is not working.While debugging also,control is not moving to btnDet_Click.what change i have to include to move control to btnDel_Click in code behind.
can anybody help?
What about
__doPostBack('btnDet','OnClick');
instead of
document.getElementById('btnDet').click();
In the server side page load event just add this code...
ClientScriptManager.GetPostBackEventReference(btnDet)
Check this msdn article here
But your code also should work.
Have you tried clicking on the button directly. Does this work?
Looks like the onclick event is generated for the btnDel button. But on submit form that contains this button was not fired. Thy to replace
document.getElementById('btnDet').click();
with
document.forms[0].fireevent('onsubmit');

Categories