How to update textbox onchange without postback? - c#

Is there a way to update a textbox without using postback? I'd like to populate the txtDFS field with the value of (100 - txtStocked) after the user enters data in the txtStocked field.
The following code works, but it uses postback so the page refreshes. Is there a way to accomplish this without refreshing the page?
<asp:TextBox ID="txtStocked" runat="server" Width="75px" AutoPostBack="true" OnTextChanged="txtStocked_TextChanged"></asp:TextBox>
<asp:TextBox ID="txtDFS" runat="server" Width="75px"></asp:TextBox>
protected void txtStocked_TextChanged(object sender, EventArgs e)
{
int stocked = Convert.ToInt16(txtStocked.Text);
int dfs = (100 - stocked);
txtDFS.Text = dfs.ToString();
txtDFS.Text = txtStocked.Text;
}

Solved this by using an UpdatePanel. Here is the updated code:
<asp:ScriptManager ID="SCManager" runat="server" />
<asp:UpdatePanel ID="SCPanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:TextBox ID="txtStocked" runat="server" Width="75px" autopostback="true" OnTextChanged="Update_Text"></asp:TextBox>
<asp:TextBox ID="txtDFS" runat="server" Width="75px" ReadOnly="True"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
protected void Update_Text(object sender, EventArgs e)
{
int stocked = Convert.ToInt16(txtStocked.Text);
int dfs = (100 - stocked);
txtDFS.Text = dfs.ToString();
SCPanel.Update();
}

Related

How do I update only a panel on check changed?

<asp:UpdatePanel id="up1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:CheckBox ID="chkParent" runat="server" autopostback="true" OnCheckedChanged="chkParent_OnCheckedChanged" />
<asp:DataList ID="ChildList" runat="server" OnItemDataBound="ChildList_OnItemDataBound">
</ContentTemplate>
</asp:UpdatePanel>
protected void chkParent_OnCheckedChanged(object sender, EventArgs e)
{
this.ChildList.DataSource = this.ChildValues;
this.ChildList.DataBind();
}
On check changed of parent, child gets updated but the entire page also loads. Any way to avoid it?

ASP dropdownlist selecedvalue is blank on button press

Hi I'm having a problem with my dropdownlist. I have goolged but nobody seems to have had the same problem. I have an updatepanel with a panel of controls within it. On button click I want to get the values from the controls the problem is on button click the selectedvalue is blank always.
here is the html
<asp:UpdatePanel runat="server" ChildrenAsTriggers="true" ID="updpnl1" EnableViewState="False">
<ContentTemplate>
<asp:Panel runat="server" ID="pnlLSP" CssClass="textBox" Width="85%" Visible="False">
<div>
<asp:DropDownList ID="ddlLSPHours" runat="server" ValidationGroup="LSP"
EnableViewState="True">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server"
ControlToValidate="ddlLSPHours" ErrorMessage="RequiredFieldValidator"
ValidationGroup="LSP">*</asp:RequiredFieldValidator>
<asp:DropDownList ID="ddlLSPMins" runat="server" ValidationGroup="LSP" EnableViewState="True">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server"
ControlToValidate="ddlLSPMins" ErrorMessage="RequiredFieldValidator"
ValidationGroup="LSP">*</asp:RequiredFieldValidator>
</div>
<div style="text-align: center">
<asp:Button ID="btnLSPDone" runat="server" CssClass="button" Text="Done" Style="margin-top: 10px"
Width="100px" ValidationGroup="LSP" OnClick="btnLSPDone_Click" />
</div>
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnLSPDone"></asp:PostBackTrigger>
</Triggers>
</asp:UpdatePanel>
c# code for populating dropdownlists
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//populate hours ddl
for (int i = 0; i < 25; i++)
{
string number = i.ToString();
if (i < 10)
{
number = "0" + i.ToString();
}
ddlLSPHours.Items.Add(number)
}
//populate mins ddl
for (int i = 0; i < 61; i++)
{
string number = i.ToString();
if (i < 10)
{
number = "0" + i.ToString();
}
ddlLSPMins.Items.Add(number);
}
}
}
protected void btnLSPDone_Click(object sender, EventArgs e)
{
string timeSelected = ddlLSPHours.SelectedValue + ":" + ddlLSPMins.SelectedValue;
}
The only problem is with your
EnableViewState="False"
in
<asp:UpdatePanel runat="server" ChildrenAsTriggers="true" ID="updpnl1" EnableViewState="False">
Any reason why you are using it?
If you remove it, will give you the selected values for the dropdowns. Something like this :
<asp:UpdatePanel runat="server" ChildrenAsTriggers="true" ID="updpnl1">
Hope this helps. Happy Coding..!!
just make EnableViewState="False" of your UpdatePanel
<asp:UpdatePanel runat="server" ChildrenAsTriggers="true" ID="updpnl1" EnableViewState="False">
it will work.

How to get link button text and value in datalist?

I have an ajaxtabcontainer control in my project which contains a datalist and in datalist, I have set the names as a link button. The datalist that I am binding to a source is containing Names and age. Now I want to use the age as a selected value as we use in dropdownlist selected value? what I want is on clicking the name link button I get its value inside a label? how to do it? what I have done is as follows:
in Default aspx:
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</ajaxToolkit:ToolkitScriptManager>
<ajaxToolkit:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="1">
<ajaxToolkit:TabPanel runat="server" HeaderText="TabPanel1" ID="TabPanel1">
<HeaderTemplate>
Names
</HeaderTemplate>
<ContentTemplate>
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="5" ForeColor="Red"
RepeatLayout="Table" >
<ItemTemplate>
<asp:LinkButton Text = '<%#Eval("names")%>' ID="lnkpro" runat="server" ForeColor="Red" OnClick="btn_Click"></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</ajaxToolkit:TabContainer>
in cs code:
protected void btn_Click(object sender, EventArgs e)
{
LinkButton myButton = sender as LinkButton;
if (myButton != null)
{
Label1.Text = myButton.Text;
}
}
protected void datalist1_ItemCommand(object source, DataListCommandEventArgs e)
{
int i;
int index = Convert.ToInt32(e.Item.ItemIndex);
LinkButton lnkid = (LinkButton)e.Item.FindControl("lnkpro");
Label1.Text = lnkid.Text;
i = Convert.ToInt32(lnkid.Text);
}
but they are not working. the btn_click is working but giving the only name not its id. the second datalist_itemcommand is not working? Thank you.
you did not have OnItemCommand="datalist1_ItemCommand" in the aspx page. datalist1_ItemCommand should fire after you set OnItemCommand property.
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="5" ForeColor="Red" OnItemCommand="datalist1_ItemCommand"
RepeatLayout="Table" DataKeyField="age" >
<ItemTemplate>
<asp:LinkButton Text = '<%#Eval("names")%>' ID="lnkpro" runat="server" ForeColor="Red" OnClick="btn_Click"></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
If you are trying to get the age field value in the datalist1_itemCommand function, you need to set DataKeyFeild= "age". And in the item command function you can do Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]) to get the value. Your item Command function will be
protected void datalist1_ItemCommand(object source, DataListCommandEventArgs e)
{
int age = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
}

Why no render partial with UpdatePanel?

I need partial render with ajax; I don't know what is wrong. ¿What is the problem?
My code:
ascx
<div id="temasatratar" onclick="__doPostBack('UpdatePanel1', '');"><h1>Temas a tratar</h1></div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="UpdatePanel1_Load">
<ContentTemplate>
<asp:Label runat="server" ID="Label1" />
</ContentTemplate>
</asp:UpdatePanel>
ascx.cs
protected void UpdatePanel1_Load(object sender, EventArgs e)
{
Random rnd = new Random();
int number = rnd.Next(0, 99999);
Label1.Text = "Best "+number;
}
Any suggest?
My application: Sharepoint - Visual web part / C# / Asp.Net / Visual Studio
I would use a fake-button that is invisible as trigger for the UpdatePanel:
<div id="temasatratar" onclick="__doPostBack('fakeButton', '');"><h1>Temas a tratar</h1></div>
<asp:LinkButton ID="fakeButton" style="display:none" runat="server" Text="foo" OnClick="fakeButton_Click" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="UpdatePanel1_Load">
<ContentTemplate>
<asp:Label runat="server" ID="Label1" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="fakeButton" />
</Triggers>
</asp:UpdatePanel>
Now this click-event is handled in an async postback when the user clicks on the div.
protected void fakeButton_Click(Object sender, EventArgs e)
{
// you are in an asynchronous postback now
}

Submit TextBox From Within A Repeater

I want to do something simple. I have a text box in a repeater Item that will allow people to add a note to the item. My code is not working, it doesn't seem like anything is happening at all.
ASPX:
<asp:Repeater ID="rptList" runat="server" ViewStateMode="Enabled">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="NoteTextBox" runat="server"></asp:TextBox>
<asp:Button ID="SubmitNote" runat="server" Text="Button" OnClick="lnkClient_Click" CommandName="AddNote" CommandArgument='<%# Eval("UID")%>'/>
<asp:Label ID="ShowNotes" runat="server" Text='<%# getNotes(Eval("UID").ToString())%>'></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
CODEBEHIND - This is what should be executed on click. I replaced my SQL code with Response.Write:
public void lnkClient_Click(object sender, EventArgs e)
{
Button btn = (Button)(sender);
string FID = btn.CommandArgument.ToString();
string note = ((TextBox)rptList.Items[0].FindControl("NoteTextBox")).Text;
Response.Write(FID + " " + note);
}
UPDATE: Changed some settings and now the only problem I am having is that the text entered client side is not passed to the command.
Try this
protected void Repeater_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("AddNote"))
{
string FID =e.CommandArgument.ToString();
TextBox txtNote=e.Item.FindControl("NoteTextBox") as TextBox;
string note=txtNote.Text;
Response.Write(FID + " " + note);
}
}
and in Mark up
<asp:Repeater ID="rptList" runat="server" OnItemCommand="Repeater_OnItemCommand" ViewStateMode="Enabled">

Categories