I have a asp ImageButton which has its corresponding Onclick event like OnClick="imgSubscriberInformation_Click", this imagebutton is inside a td, so I want to call imgSubscriberInformation_Click(object sender, ImageClickEventArgs e) on click of td, how can I do this? the td doesnot support serverside click event.
Also, I need the imageURL of imagebutton to be passed on every click.
aspx code:
aspx.cs code:
protected void imgSubscriberInformation_Click(object sender,ImageClickEventArgs e)
{
hidCtrl.Value = "false";
if (hidSubscribeInfo.Value == "")
{
if (imgSubscriberInformation.ImageUrl == "Images/downarrow.png")
{
LoadUserControl();
PopulateData();
imgSubscriberInformation.ImageUrl = "Images/uparrow.png";
}
}
else if (hidSubscribeInfo.Value != "")
{
imgSubscriberInformation.ImageUrl = "Images/uparrow.png";
LoadUserControl();
PopulateData();
hidSubscribeInfo.Value = "";
}
}
You could do this entirely with jQuery/javascript. Grab all the TD's (or specify them by class) and assign to them an onclick event to trigger the click of the image button within.
//all TD's
$("#mytable td").on( "click", function() {
$(this).find('img').trigger( "click" );
});
//or
// by class
$(".tdWithImgButton").on( "click", function() {
$(this).find('img').trigger( "click" );
});
Guys i found a solution,
i moved the onClient click function to TD
and inside the javascript function i did this "__doPostBack("imgSubscriberInformation", "");"
this will call the click event of the imageButton.
Thanks for your time.
Related
I have some validation code in my searchbutton click event and have been having problems with it having to be clicked twice to work.
Asp Code:
<asp:Button ID="SearchButton" runat="server" Text="Search" Width="148px" OnClick="SearchButton_Click" style="height: 35px" />
Code Behind
protected void SearchButton_Click(object sender, EventArgs e)
{
string title = TitleSearch.Text;
Regex rgx = new Regex("^[0-9A-Za-z ]+$");
if (title != "" && !rgx.IsMatch(title))
{
ErrorLabel.Text = "Special characters are not allowed";
}
else
{
SearchButton.PostBackUrl = "results.aspx";
}
}
does the textbox have postback ? becaus if you change the text in the textbox it will do postback when you leave the textbox.so if you click the button the postback of the textbox fires.
I would check the textbox with java
Add in you page load event "Change EditGroup to the TextBoxName you want to check"
EditGroup.Attributes.Add("onchange", "return SomeTextChanged();");
This will add an onchange event to the textbox and it will call the java function in your aspx page when you click the button
Then in your aspx page you add "Again change EditGroup to the name of the TextBox you want to check"
<script type="text/javascript">
function SomeTextChanged() {
var Entered = document.getElementById('<%= EditGroup.ClientID %>');
if (Entered.value != "" && !Entered.value.match("^[0-9A-Za-z ]+$"))
{
alert("Special characters are not allowed");
document.getElementById('<%= EditGroup.ClientID %>').value = '';
}
else
{
}
}
</script>
So if you enter something that is not allowed you will get a message saying "Special characters are not allowed"
This will also stop you page from executing the rest of the code in the button click event.
And you also need to empty the textbox"i know this is maybe not the best way but if you don't empty the textbox and the user will click the button again it will not run the java code because the text didn't change"
So if if the text is good the java script will do nothing and the button click event will fire
Previously when my RadGrid was not a batch edit grid I was able to use the grid's AddNewRecord button to redirect the user to another page with the following code:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "InitInsert")
{
Response.Redirect(redirectUrl + "?ProductID=" + this.ProductId);
}
}
After I made my grid a batch edit grid the Add New Button doesn't go into the ItemCommand event anymore and instead tries adding an inline insert record row to the grid. Is there anyway I can still use this button and override its functionality to still redirect the user?
So I've tested this and confirmed what I suspected in the comments. When EditMode="Batch", the "Add New Record" button, along with others, no longer cause a postback. You can override this by removing the JavaScript of the OnClientClick in the RadGrid1_ItemCreated like so:
Add this to your RadGrid1 attributes:
OnItemCreated="RadGrid1_ItemCreated"
Code behind (note: there is actually a Button AND a LinkButton):
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item.ItemType == Telerik.Web.UI.GridItemType.CommandItem) {
//This is the icon with the plus (+) sign unless you've changed the icon
Button iconButton = e.Item.FindControl("AddNewRecordButton");
if (iconButton != null) {
iconButton.OnClientClick = "";
}
//This is the words "Add New Record" or whatever you've called it
LinkButton wordButton = e.Item.FindControl("InitInsertButton");
if (wordButton != null) {
wordButton.OnClientClick = "";
}
}
}
This should allow the postback to happen and the code you posted should be able to run.
I'm using a LinkButton and a DropDown.
When I click on the LinkButton the DropDown appears.
After selecting a DropDown value, I want a confirmation box called from JavaScript to appear, ensuring that the value is changed.
I'm calling this script in the second if condition, but it's not working.
After the confirmation I want to change the other value and exit from the condition.
protected void lnkbtnSave_Click(object sender, EventArgs e)
{
if ((ddlHiringManager.SelectedItem != null &&
(ddlHiringManager.SelectedItem.Text != lblHiringManager.Text)) &&
(Convert.ToInt32(ddlHiringManager.SelectedValue)) != -1)
{
if (ClientScript.RegisterStartupScript(typeof(Page), "Confirm", "<script type='text/javascript'>Confirm('Are you sure you want to change Hiring Manager for this requirement.');</script>"))
{
ClsClientManager objClientManager = new ClsClientManager();
if (objClientManager.UpdateHRManagerByReqID(Convert.ToInt32(hdnReqId.Value), Convert.ToInt32(ddlHiringManager.SelectedValue)) > 0)
{
lblShowHiringManager.Text = ddlHiringManager.SelectedItem.Text;
}
}
}
else
{
ClientScript.RegisterStartupScript(typeof(Page), "SymbolError", "<script type='text/javascript'>alert('Please Select Hiring Manager !');</script>");
}
}
You cannot use the result of RegisterStartupScript method.
Change ASPX page code for the LinkButton as given below
<asp:LinkButton ID="lnkbtnSave" runat="server" OnClick="lnkbtnSave_Click"
OnClientClick="javascript: return confirm('Are you sure you want to change Hiring Manager for this requirement.');">Save</asp:LinkButton>
I have added the client side click event.
On clicking the LinkButton you will get the confirmation box. The page will postback only if you click OK in the confirmation box.
Please refer this Code Snippet. On dropdown selected index change event
protected void ddlTest_SelectedIndexChanged(object sender, EventArgs e)
{
string str = "Are you sure, you want to upload leave ?";
this.ClientScript.RegisterStartupScript(typeof(Page), "Popup", "ConfirmApproval('" + str + "');", true);
}
And for Client Side declare that method.
<script type="text/javascript">
function ConfirmApproval(objMsg) {
if (confirm(objMsg)) {
$('#divUploadLeave').fadeTo('slow', .6);
return true;
} else {
$('#divUploadLeave').fadeTo('slow', 1);
return false;
}
}
Hope It helps you.
Still if you want all things on Client Side please let me know.
Please add return before Confirm this will solve your issue.
**if (ClientScript.RegisterStartupScript(typeof(Page), "Confirm", "<script type='text/javascript'>return Confirm('Are you sure you want to change Hiring Manager for this requirement.');</script>"))**
I have a DropDownList which i am binding on page load. i dont have any buttons or anything. as soon as user selects the value in dropdown i need to show that value in label. i am not sure why this is not working. please help.
public string SelectedStore { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindStoresList();
}
}
protected void BindStoresList()
{ storeDDList.AppendDataBoundItems = true;
storeDDList.Items.Add(new ListItem("Select store", "-1"));
TempCollection stores = TempDataSource.LoadForCriteria("ALL", "Code ASC");
storeDDList.DataSource = stores;
storeDDList.DataTextField = "DisplayName";
storeDDList.DataValueField = "Code";
storeDDList.DataBind();
}
protected void storeDDList_SelectedIndexChanged(object sender, EventArgs e)
{
SelectedStore = storeDDList.SelectedValue.ToString();
selectedItem.Text = SelectedStore;
}
I dont need any kind of jquery stuff as i am going to add gridview which binds depending on the value of dropdown..
****** EDITS *******
if i set AutoPostBack=True then on page refresh my DropDownList doesn't bind at all as you can see in Page_Load Method, it will not call BindStoresList() method.
***** ANSWER *****
For people who might get stuck with this..
i was setting the EnableViewState to True for the DropDownList, so after page refreshes the SelectedValue was getting lost. after removing the EnableviewState and setting AutoPostBack to Ture working fine...
You can Use JavaScript . Set the OnChange attribute for your DropDownList to call a JS function and Change your label text there:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindStoresList();
storeDDList.Attributes["onChange"] = "ChangeLabelText();";
}
}
JS function :
<script type="text/javascript">
function ChangeLabelText() {
var lbl = document.getElementById("<%=lbl.ClientID%>");
var ddl = document.getElementById("<%=ddl.ClientID%>");
lbl.innerHTML = ddl.options[ddl.selectedIndex].text;
}
</script>
You have to set the AutoPostBack=True for the dropdown, it will automatically send the call to server side without need to extra button.
You may do it using Javascript, handle the OnChange event of the DropDownList and set the text of the label you want
<asp:DropDownList ID="ddl" runat="server" onchange="ddl_change(this.value)"/>
<script language="javascript" type="text/javascript">
function ddl_change(value)
{
var lbl = document.getElementById('<%= yourlabel.ClientID %>');
lbl.value = value;
}
</script>
Good luck.
I have a GriView and implemented RowDataBound event like below
protected void gvwSearchResult_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int index = e.Row.RowIndex;
string IdValue = gvwSearchResult.DataKeys[index].Value.ToString();
Button _btnCheque = (Button)e.Row.FindControl("btnCheque");
_btnCheque .OnClientClick = "<script type='text/javascript'>if(confirm('Do you want to continue?')){window.location='Cheque.aspx?ID=" + IdValue.ToString()+"';}</script>";
}
}
When I click on button, there is a script error thrown. any idea on my script formatting?
What is the error you are getting?
Try using the same without the <script> tag (see example in MSDN)...
You don't need the script tags in the OnClientClick - just put the javascript directly.