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');
Related
I have an ASP.NET project with a simple webpage.When I load the page the textbox display "Hello". When I click the "btnUpload" the text goes away.
I tried !IsPostBack in the Load_Form function gut the just make the text stay the same.
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "Hello";
}
protected void btnUpload_Click(object sender, EventArgs e)
{
TextBox1.Text = "Good Bye";
}
This is because you have not used
if(!IsPostBack) { }
inside Page_load() method
so modify your code as:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) {
TextBox1.Text = "Hello";
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
TextBox1.Text = "Good Bye";
}
Because, you have not used this, thats why, on the postback of button it changes the value to TextBox1.Text = "Good Bye"; but it follows completing the postback with Page_Load() so it changes it again to TextBox1.Text = "Hello"; that's the reason default value of Page_Load() is shown after render.
You have to use the following condition in the Page_Load event handler, not in the Load_Form:
if (!IsPostBack) {
TextBox1.Text = "Hello";
}
Your button click event handler is ok.
Hope it helps!
Please see below code :
Default.aspx:
<asp:TextBox ID="text1" runat="server"></asp:TextBox>
<asp:Button ID="btn" runat="server" OnClick="btn_Click" />
Make sure aspx page contain same code for button as shown above.
I am working on asp.net project. I have a search textbox in it. when I search first time it work properly. Then after refresh the page textbox value is not clearing.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
txtEmployeeID.Text = string.Empty;
}
txtEmployeeID.Focus();
}
If you are just refreshing by pressing F5 or Ctrl + R then it is considered as just a page refresh.
There is a difference in refresh and a postback.
Please refer the below article
http://www.codeproject.com/Articles/68371/Detecting-Refresh-or-Postback-in-ASP-NET
try this code
in your mark up set autopostback in your textbox as true
protected void Page_Load(object sender, EventArgs e)
{
if(Page.IsPostBack)
{
txtEmployeeID.Text = string.Empty;
}
txtEmployeeID.Focus();
}
just remove "!" in your if statement, or remove the if statement.
I have code Page_Load and btnGonder_Click.I records the some data to database when I click the btnGonder.And I get and show data in the datalist the code which inside the page_load.The problem is that When I every refresh the page,it runs btn_Gonder_Click events and so it records the same data to database.my code is below.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Cevap cevaplar = new Cevap();
int soru_id = Convert.ToInt32(Request.QueryString["Soru_Id"]);
cevaplar.soru_id = soru_id;
DataTable dtcvp = new DataTable();
dtcvp = cevaplar.Cevaplarive_CevapVerenleri_Getir();
datalistcevaplar.DataSource = dtcvp;
datalistcevaplar.DataBind();
}
}
protected void btnGonder_Click(object sender, EventArgs e)
{
Users kullanicim = new Users();
HttpCookie bilgiler = Request.Cookies["Kullanicicookie"];
kullanicim.mail = bilgiler["mail"];
int donenkullaniciid = kullanicim.Kullanici_Id_Donder();
cevaplar.cvpveren_id = donenkullaniciid;
Sorular sorular = new Sorular();
sorular.Cevap_Sayisi_Artir(soru_id);
bool durum = cevaplar.Cevap_Ekle();
if (durum)
{
lblDurum.Text = "Cevabınız Eklenmiştir";
}
else
{
lblDurum.Text = "Cevabınız Eklenmemiştir";
}
DataTable dtcvp = new DataTable();
dtcvp = cevaplar.Cevaplarive_CevapVerenleri_Getir();
datalistcevaplar.DataSource = dtcvp;
datalistcevaplar.DataBind();
}
Page refresh re-fires the same event which has caused the previous postback , so if you refresh the page after clicking the button then button click event will fire. Now to avoid that you have gor multiple solutions. see here
Use this.IsPostBack inside btnGonder_Click() to check the status of submission and return
If you don't want to resubmit the page on refresh then from btnGonder_Click you need to redirect to the same page instead of directly binding data. As this is the basic problem with the Asp.net application.
Do Response.Redirect and pass the name of the page.
If you have already clicked on a button which caused a post back. Next onwards pressing F5, the browser will ask you RESEND which means click the button again. This is normal browser behaviour
Basically you need a workaround to check if it is a page refresh or an actual button click. You can do as below:
The key here is ViewState["IsPageRefresh"] & Session["IsPageRefresh"]. Note that ViewState needs to be enabled on the page for this to work; if ViewState is not enabled then a hidden field may be used.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session["IsPageRefresh"] = Server.UrlDecode(System.DateTime.Now.ToString());
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
ViewState["IsPageRefresh"] = Session["IsPageRefresh"];
}
protected void btnPost_Click(object sender, EventArgs e)
{
if (Session["IsPageRefresh"].ToString() == ViewState["IsPageRefresh"].ToString())
{
//Put your database INSERT code here
Session["IsPageRefresh"] = Server.UrlDecode(System.DateTime.Now.ToString());
}
}
One simple trick but may be not a standard practice I too agree is below:
Don’t disable the button in the server side. After the page has finished loading the button works as usual.
<head runat="server">
<title></title>
<script type="text/javascript">
function checkButton() {
var btn = document.getElementById("btnGonder");
btn.disabled = true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnGonder" runat="server" onclick="btnGonder_Click"
OnClientClick="checkButton();" />
</div>
</form>
</body>
im having a gridview in only page and on clicking a button column field Gridview1_RowCommand gets executed.What im stuck at is on how to pass a single refno(which is in the grid) to the popup page.i want to use this refno to generate a query
help on this will really be appreciated
the below code i wat i used and im stuck here..
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Page.RegisterStartupScript("test", "<script language='javascript'>Openpopup();</script>");
}
<script language='javascript'>
var popupobj;
function Openpopup()
{
popupobj = window.open("popup.aspx", "_blank", "width=1000,height=500,statusbar=no,toolbar=no,scrollbars=no,resizable=no,navbar=no,screenX=800,screenY=800top=100,left=100");
popupobj.focus();
}
</script>
Since you are using RowCommand event, to be able to send this value to the Popup you need to set the RowIndex as a CommandArgument of the Button you are clicking to open the popup.
Set the Value(refno) to passed to Popup in DataKey.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
var Button1= e.Row.FindControl("Button1") as Button;
Button1.CommandName = "ButtonClick";
// cast RowIndex in line code to string if e.CommandArgument accepts string
Button1.CommandArgument = e.Row.RowIndex;
}
Now in the RowCommand
protected void GridView1_RowCommand(object sender, GridViewRowCommandArgs e)
{
if(e.CommandName == "ButtonClick")
{
var refno = GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)]["refno"];
Page.RegisterStartupScript("test", "<script language='javascript'>Openpopup(' + refno + ');</script>");
}
}
This way now you have refno in the Javascript function argument. Pass this argument as a querystring in the Javascript popup function.
This way you can access refno in the Popup page.
I have a dropdown.Basing on this dropdown i shud load another dropdown.i wrote code in on selected index changed event in .cs page.Depending on selectd item in first dropdown i need to give a validation in javascript.let us think i should show an alert.Can any one help me out.
in .aspx page
<asp:DropDownList ID="drpdes" runat="server" onselectedindexchanged="drpdes_SelectedIndexChanged"></asp:DropDownList>
in .cs page
protected void Page_Load(object sender, EventArgs e)
{
drpdes.AutoPostBack = true;
}
protected void drpdes_SelectedIndexChanged(object sender, EventArgs e)
{
int flgchk = 0;
if(drpdes.selectedvalue == "0")
{
flgchk = 1;
}
// my code for binding second dropdown
}
when flgchk = 1 i need to show an alert from javascript.i wrote a function for alert in javascript.But i am not understanding where to call it.
something like this :)
protected void drpdes_SelectedIndexChanged(object sender, EventArgs e)
{
int flgchk = 0;
if(drpdes.selectedvalue == "0")
{
flgchk = 1;
/// this will call your alert method.
string errorScript = "<script type=\"text/javascript\">" +
"YourFunctionNameHere() " +
"</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", errorScript);
}
// my code for binding second dropdown
}
If you remove AutoPostback on your dropdownlist you can validate client side. Something like onchange = YourJSMethod(this.value).