drop down list not post back-javascript - javascript

I have a tabpanel with a dropdownlist control on the 4th tab but when I select a item, the postback not occur even autopostback property is true. Is there any way to postback the page when item is selected?
I think this use the javascript code to postback the drop down list please suggest.
Any ideas ?
<asp:DropDownList ID="ddlrole" runat="server" Width="151px" AutoPostBack="True"
onselectedindexchanged="ddlrole_SelectedIndexChanged">
</asp:DropDownList>
code behind is:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//code goes here
}
}
protected void ddlrole_SelectedIndexChanged(object sender, EventArgs e)
{
st = int.Parse(ddlrole.SelectedValue);
}
ddlrole_SelectedIndexChanged is not fire when an item is selected from drop down list?
I use the javascript code to prevent this tab to return to first tab:
<script type="text/javascript">
$(function () {
$("#tabContainer").tabs();
var currTab = $("#<%= HFCurrTabIndex.ClientID %>").val();
$("#tabContainer").tabs({ selected: currTab });
});
</script>

Related

while loading image on button click using update process it allows other buttons click

Inside update panel I have 40 buttons: Button1, Button2, Button3 etc
On each button click, text will be stored to a variable as below:
protected void Button1_Click(object sender, EventArgs e)
{
string text1="You clicked button1";
}
When I click on Button1 the ajax gif inside updateprocess will start but at the same time it allows other buttons to be clicked.
How do I prevent it?
Else is there any other way to do so other than using updateprocess?
After discussion we had in comments, if I understood you correctly you can use OnClientClick to disable buttons when user clicked in client side. After click function is completed you can enable all of these buttons from server side.
Client side:
<script>
function DisableButtons() {
$("#<%=btn1.ClientID %>").attr('disabled', true);
$("#<%=btn2.ClientID %>").attr('disabled', true);
//other buttons
}
</script>
<asp:Button ID="btn1" Text="Button 1" runat="server" OnClick="btn1_Click" OnClientClick="DisableButtons();" UseSubmitBehavior="false" />
<asp:Button ID="btn2" Text="Button 2" runat="server" OnClick="btn2_Click" OnClientClick="DisableButtons();" UseSubmitBehavior="false" />
<%--Other buttons--%>
Note that you need to add UseSubmitBehavior="false" as well to buttons.
Server side:
protected void btn1_Click(object sender, EventArgs e)
{
//other processes
SetButtonsEnable();
}
protected void SetButtonsEnable()
{
btn1.Attributes.Remove("disabled");
btn2.Attributes.Remove("disabled");
//other buttons
}

cannot get the newest button name in C#

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
}
}

How to get value from TextBox in TemplateField when value change by javascript c# asp.net

script(update value)
$("#txtPlanDate").datepicker();
design page
<asp:TextBox ID="txtPlanDate" ReadOnly="true" runat="server" Text='<%# Bind("PlanDate", "{0:dd/MM/yyyy}") %>'></asp:TextBox>
code behind (get value)
protected void GridView1_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{TextBox txtPlanDate = (TextBox)GridView1.Rows[e.RowIndex].Cells[5].FindControl("txtPlanDate");}
I suppose you have textbox inside gridview templatefield . Then You need something like this
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtPlanDate = (e.Row.FindControl("txtPlanDate") as TextBox);
}
It works OnRowDatabound event.Not tested OnRoUpdating event but it should work fine.

I want to update values in drop down box without page refreshing in ASP.Net

I want ASP .NET code for as example-
As I have four drop down box, when i will select the value in first box then in second box filtered value according to first box should be without any page refresh please help me.
Example-
I am giving you the example of country, state, city. when i will select the country then in second box all states should be updated according to country as when i will select the state then all city name should be updated in third box.
This process should be without refreshing the page.
If you are using webforms,you can use an UpdatePanel in your webforms and giving drop downlist in the update panels..
Please find the sample code
List<Districts> list = new List<Districts>();
Districts item = new Districts();
protected void Page_Load(object sender, EventArgs e)
{
if (drd1.SelectedItem.Value == "Kerala")
{
item.sname = "Ekm";
item.id = 1;
list.Add(item);
Districts item1 = new Districts();
item1.sname = "thr";
item1.id = 2;
list.Add(item1);
}
else
{
item.sname = "Coimbatore";
item.id = 1;
list.Add(item);
Districts item1 = new Districts();
item1.sname = "Chennai";
item1.id = 2;
list.Add(item1);
}
}
public class Districts
{
public string sname { get; set; }
public int id { get; set; }
}
protected void drd1_SelectedIndexChanged(object sender, EventArgs e)
{
drd2.DataSource = list;
drd2.DataTextField = "sname";
drd2.DataValueField = "id";
drd2.DataBind();
}
and your page looks like this
<asp:ScriptManager ID="scr" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="upd" runat="server" >
<ContentTemplate>
<asp:DropDownList ID="drd1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="drd1_SelectedIndexChanged">
<asp:ListItem Text="Kerala" Value="Kerala">
</asp:ListItem>
<asp:ListItem Text="TN" Value="TN"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="drd2" runat="server" Width="100"></asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>

How do I get DropDownList selected value from user in Asp.Net(selectedIndexChange)

How do I go about getting the selected value from the DropDownList in asp.Net using selectedIndexChanged with an update Panel? I've tried but when I add the AutoPostBack to my DropDownList, it sends me to an error page saying it cannot find the resource.(doesn't even hit my "selected_IndexChanged") I have the hiddenField being assigned the selected value.
Here is my DropDownList:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<asp:DropDownList runat="server" ID="ddlCaseFilesNew" DataSourceID="dsCaseFiles"
DataTextField="Display" DataValueField="FileID" OnPreRender="ddl_PreRender" Width="300px"
AutoPostBack="true" OnSelectedIndexChanged="ddlCaseFilesNew_SelectedIndexChanged" Visible="False">
<asp:ListItem>Item 1</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlCaseFilesNew" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
When I click on a value inside the DropDownList it sends me to a "cannot find resource" page. I have no idea why. It doesn't even touch my "onSelectedIndexChanged"
<script runat="server">
protected void ddlCaseFilesNew_SelectedIndexChanged(object sender, EventArgs e)
{
hidNewCaseFile.Value = ddlCaseFilesNew.SelectedItem.Value;
}
</script>
In your SelectedIndex function, use
string sValue = ((DropDownList)sender).SelectedValue;
Since it's in an update panel, you likely won't have access to it via the designer (this.DdlId).
Based on your comment in the OP, this is likely what you need:
protected void ddlCaseFilesNew_SelectedIndexChanged(object sender, EventArgs e)
{
hidNewCaseFile.Value = ((DropDownList)sender).SelectedValue;
}
You also need to move your hidden field into the ContentTemplate.
Use:
protected void myDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
var res = this.myDropDownList.SelectedValue;
}
Or:
protected void myDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
var ddl = sender as DropDownList;
var res = ddl.SelectedValue;
}
Edit 1
You need to understand that the partial page rendering occurs for all controls inside an UpdatePanel, in your case, you are trying to set the Value property of hidNewCaseFile control which does not appear to be inside the UpdatePanel therefore, its value will never get updated

Categories

Resources