Currently, I have a working code that selects and transfers gridview row data in a textbox. Here's the code
protected void grdRP_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = grdLocation.SelectedRow;
hdnSearchedLoc.Value = grdLocation.Rows[grdLocation.SelectedIndex].Cells[1].Text;
txtSearchedLoc.Text = HttpUtility.HtmlDecode(row.Cells[2].Text);
}
It works well functionality and performance (speed) wise when it is local. I have seen the difference in speed when I upload it to the web server. it takes 5 seconds and more before it transfer the row data to textbox.
Now, I am thinking of alternative way to make it faster by using javascript since it is on client-side. I have seen a sample code but it is only on selection method but the transfer of row data is still the same speed. Here's the code
javascript
function setMouseOverColor(element)
{
oldgridSelectedColor = element.style.backgroundColor;
element.style.backgroundColor='#C0C0C0';
element.style.cursor='hand';
//element.style.textDecoration='underline';
}
function setMouseOutColor(element)
{
element.style.backgroundColor=oldgridSelectedColor;
element.style.textDecoration='none';
}
aspx.cs
protected void grdRP_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Style["display"] = "none";
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] =
"javascript:setMouseOverColor(this);";
e.Row.Attributes["onmouseout"] =
"javascript:setMouseOutColor(this);";
e.Row.Attributes["onclick"] =
Page.ClientScript.GetPostBackClientHyperlink
(this.grdRP, "Select$" + e.Row.RowIndex);
}
}
is there other javascript way of doing that?
Related
I have written a code for button click that should retrieve data from the database and display the data row wise as required. It is working Fine for the first two clicks and it is not firing for the third time... I really don't understand why Please any help.
thank you in advance
The button code
protected void NextButton_Click(object sender, EventArgs e)
{
c++; //integer created to iterate through rows of datatable
qno++; //just counts the rows begining from 1 and displays on page
SqlCommand lque = new SqlCommand("select * from questions where course='" + cour + "' and [group]='" + gr + "' and semester='" + sem + "'", con);
IDataReader rque = lque.ExecuteReader();
if (rque.Read())
{
DataTable dt = new DataTable();
dt.Load(rque);
QuestionNo.Text = Convert.ToString(qno);
Question.Text = dt.Rows[c].Field<string>(4);// only displaying the required columns.
RadioButton1.Text = dt.Rows[c].Field<string>(5);
RadioButton2.Text = dt.Rows[c].Field<string>(6);
RadioButton3.Text = dt.Rows[c].Field<string>(7);
RadioButton4.Text = dt.Rows[c].Field<string>(8);
}
}
source code for the button
<asp:Button ID="NextButton" runat="server" Text="Next" Width="111px" OnClick="NextButton_Click" />
i also checked the data is loaded without any errors into the datatable by passing the datatable as a source to the gridview, it shows all the rows in gridview but in the labels the first 2 rows only displaying. I also chekced the counting varible is only increasing 2 times.enter image description here
a few rows from the table that i am retrieving
Your variables(c for example) will be reset to 0 on every request(button-click). Http is stateless. So you need to persist this value somewhere else(i.e. Session, ViewState, Hiddenfield, etc).
For example with Session, which you should only use if this site has not too much traffic:
private int CurrentRowIndex
{
get
{
if (Session["CurrentRowIndex"] == null)
Session["CurrentRowIndex"] = 0;
return (int)Session["CurrentRowIndex"];
}
set => Session["CurrentRowIndex"] = value;
}
protected void NextButton_Click(object sender, EventArgs e)
{
int currentRowIndex = ++CurrentRowIndex;
// maybe you need this also for your other variables
// ...
}
You should also not read all rows if you only want one, you should modify your sql query. If you use MS SQL-Server you could use ROW_NUMBER function to select only the required row from DB.
my asp project has a asp button which named 'Save'. However, i need to change it to 'Update' in jQuery to do other thing in one button.
The problem is the C# onclick code is still read it as Save..
Code:
//Main
<asp:Button ID="btnSubmit" runat="server" Text="Save" />
//JQuery
$("#<%=btnSubmit.ClientID %>").val("Update");
//C#
var button = sender as Button;
var buttonName = button.Text; //Save
Update1
Question is:
I need to change the button name in JQuery
$("#<%=btnChangeName.ClientID %>").click({
$("#<%=btnSubmit.ClientID %>").val("Update"); //Change Save to Update
});
Then use it in C#:
protected void btnSubmit_Click(object sender, EventArgs e)
{
var button = sender as Button;
var buttonName = button.Text;
if (buttonName == "Save")
{//something}
else
{//something}
}
if you want to change the Name to Update1 in Jquery
try like this
$("#<%=btnSubmit.ClientID%>").val('Update');
Event you neeed to use is OnClientClick
Usage example
OnClientClick="onupd();return false;"
Update
Decalre a Hidden FIeld
<asp:HiddenField runat="server" ID="hdntest" Value="0" />
assign the button change value to update and assign it to the hidden field
not access that hidden value in your code behind.
you can assign Button value to Hidden Field like this
$("#<%=btnSubmit.ClientID %>").val("Update");
$("#<%=hdntest.ClientID%>").val($("#<%=btnSubmit.ClientID%>").attr("value"));
If you change the Button.Click value, then it will move to a different method onClick;
private void Form1_Load(object sender, EventArgs e)
{
Button b = new Button();
b.Name = "btnSubmit";// creating here for intellisense - you would create on your front-end
bool recordExists = true;
Button b1 = Controls.Find("btnSubmit", true)[0] as Button;
//the above is for winforms - you'd have to change for web form
b1.Text = recordExists ? "Update" : "Save";
b1.Click += doUpdateOrSave;
}
protected void doUpdateOrSave(object sender, EventArgs e)
{
Button b = sender as Button;
if (b.Text == "Save")
{
//save
}else
{
//update
}
}
I have a webBrowser control on my C# forms app,which works perfectly.
However, the url I am navigating to has JavaScript popups with text on them during interaction.
How do I capture the text on the JavaScript popup boxes?
For example, the alert says "no more food" I want to capture the text "no more food" and pass to a label .
below is my click event code and OnDocumentCompleted event handler
private void iTalk_Button_21_Click(object sender, EventArgs e) //click event
{
Username = username.Text.Trim();
Password=password.Text.Trim();
webBrowser1.Navigate("someurl");
webBrowser1.DocumentCompleted += WebBrowser1_DocumentCompleted;
//MessageBox.Show(Username);
}
private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
tabControl1.SelectedTab = tabPage2;
HtmlDocument doc = this.webBrowser1.Document;
doc.GetElementById("ctl00_MainContent_Login1_UserName").SetAttribute("Value", Username);
doc.GetElementById("ctl00_MainContent_Login1_Password").SetAttribute("Value", Password);
doc.GetElementById("ctl00_MainContent_Login1_LoginButton").InvokeMember("click");
}
I'm trying to disable the two list boxes on load of my web form, but the list boxes are not disabling. I tried the code below but it's still not working on my case. Please let me know where I'm going wrong.
DESIGNER CODE
<telerik:RadListBox ID="lbSelectedDepartmentContacts" runat="server" Width="220px" AutoPostBackOnTransfer="true" AutoPostBackOnReorder="true" AutoPostBackOnDelete="true" CheckBoxes="false" AllowTransfer="true" AllowTransferOnDoubleClick="true" TransferToID="lbDepartmentContacts"
Height="276px" SelectionMode="Single" AllowReorder="True">
<ButtonSettings ShowTransfer="false" ShowTransferAll="false" ReorderButtons="Common" ShowReorder="true" />
</telerik:RadListBox>
</td>
</tr>
<tr>
<td align="right">
<telerik:RadListBox ID="lbDepartmentContacts" runat="server" Width="200px" CheckBoxes="false" AutoPostBackOnTransfer="true" AllowTransfer="true" AllowTransferOnDoubleClick="true" TransferToID="lbSelectedDepartmentContacts"
Height="250px" SelectionMode="Single">
<ButtonSettings TransferButtons="Common" ShowTransfer="false" ShowTransferAll="false" />
</telerik:RadListBox>
BACKCODE
public void DisableControls()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(#"<script language='javascript'>function DisableControls(){");
sb.Append(#"var lbl = document.getElementById('lbSelectedDepartmentContacts').disabled = true;");
sb.Append(#" var Txt = document.getElementById('txtDepartmentCategoryContactsFilter').disabled = true;");
sb.Append(#" var btn = document.getElementById('RadButton1').disabled = true;");
sb.Append("}</script>");
if (!ClientScript.IsClientScriptBlockRegistered("JSScriptBlock"))
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "JSScriptBlock",
sb.ToString());
}
string funcCall = "<script language='javascript'>DisableControls();</script>";
if (!ClientScript.IsStartupScriptRegistered("JSScript"))
{
ClientScript.RegisterStartupScript(this.GetType(), "JSScript", funcCall);
}
}
Tried Also:
Page.ClientScript.RegisterStartupScript(this.GetType(), "text", "DepartmentLoad();", true);
JAVASCRIPT CODE
function DepartmentLoad(){
document.getElementById("lbSelectedDepartmentContacts").disabled = true;
document.getElementById("lbDepartmentContacts").disabled = true;
document.getElementById("txtDepartmentCategoryContactsFilter").disabled = true;
ducument.getElementById("RadButton1").disabled = true;}
C# only approach to disable your 2 listboxes...
Delete or comment out your existing DisableControls() method then add the following...
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack) return;
DisableControls();
}
private void DisableControls()
{
lbSelectedDepartmentContacts.Enabled = lbDepartmentContacts.Enabled = false;
}
You should generally:
Use Sys.Application.Load to register your script. Calling the function earlier will cause problems with using the client-side API of the IScriptControls: http://msdn.microsoft.com/en-us/library/bb383829.aspx and http://docs.telerik.com/devtools/aspnet-ajax/controls/window/troubleshooting/executing-javascript-code-from-server.
use the API the Telerik control provides: http://docs.telerik.com/devtools/aspnet-ajax/controls/listbox/client-side-programming/objects/radlistbox-object.
There seems to be no set_enabled() or set_disabled() method on the client (at least at the time of writing), so this seems to be impossible with JS, so you should go with the other answer that tells you to use server code.
I am trying to provide an ok cancel popup 'if' the gridview has no rows and I thought this would work like a client script, but when I hit ok the control skips rest of the method. Is there anyway I could perform this validation or alert and move the control to the next statement?
protected void btnRunD_Click(object sender, EventArgs e)
{
try
{
int iESStatus = 0;
int iRunStatus = 0;
ApplicationUser au = (ApplicationUser)Session["ApplicationUser"];
if (UploadFileGrid.Rows.Count <= 0)
{
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "err_msg", "confirm('No new data files have been uploaded. Are you sure you want to run EYDS Processing Module?');",
//true);
}
if (au.RoleID != 2) //RoleID is not Admin!
{
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "err_msg", "alert('You do not have enough privileges to use this functionality!');",
true);
}
else
{.........}
}
The part I'm having difficulty with is here:
I am trying to provide an ok cancel popup 'if' the gridview has no rows
if (UploadFileGrid.Rows.Count <= 0)
{
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "err_msg", "confirm('No new data files have been uploaded. Are you sure you want to run?');",
}
Supposing your button is something like this:
<asp:Button ID="btnDoSomething" runat="server" Text="Do Something" OnClientClick="return GetConfirmation();" OnClick="btnDoSomething_Click" />
then add a javascript function as below:
function GetConfirmation() {
var rowscount = document.getElementByID(<%=UploadFileGrid.ClientID%>).rows.length;
return rowscount > 0 || confirm('No new data files have been uploaded. Are you sure you want to run?');
}
then it'll be postbacked just when the user press ok when confirm button is shown.
You can do it using javascript or jquery:
$("#<%=btnApprove.ClientID %>").click(function () {
if ($("#<%=UploadFileGrid.ClientID %> ").find("tr").length == 0) {
if (confirm('No new data files have been uploaded. Are you sure you want to run?')) {
return true;
}
else {
return false;
}
}
});
and when user press OK,your page will be postbacked,you can write the code which you want to execute after that.
I hope this code will help you.