Adding a Value Attribute to on a AspxButton - asp.net

<dxe:ASPxButton ID="getProgram" runat="server" Text="Programs" SkinID="MenuButton" OnClick="Button_Click">
</dxe:ASPxButton>
Here is my Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
this.getProgram.Attributes.Add("Value", "View Progams Button");
}
I tried this it didn't work. Is there any other way I can achieve this?

I don't know what a <dxe:ASPxButton> is but a regular <asp:Button> has its value property set via the 'Text' attribute: -
<asp:Button ID="button" runat="server" Text="click me :)" />

Related

reset /clear button?

hi im using visual studio 2010 and asp
i just created simple form with submit and clear button
and i want to clear all textboxes
the sumbit button is working. but idk for clear button.
note:
the button1_click is subit button,
button2_click is clear button
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (IsPostBack)
Response.Write("Success");
}
protected void Button2_Click(object sender, EventArgs e)
{
//////// -- i dont know what to put here huhuhu help
}
}
Assuming you have text boxes called Text1, Text2, Text3 (etc.) -->
<asp:textbox id="Text1" runat="server" />
<asp:textbox id="Text2" runat="server" />
<asp:textbox id="Text3" runat="server" />
then to clear the entry in the text boxes server side, you want to have the following code in the Button2_click() method (assumed to be the Clear button) -->
Text1.Text = "";
Text2.Text = "";
Text3.Text = "";
Assuming that the ID of the textboxes are TextBox1 and TextBox2, here's how it's done:
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
}
EDIT
To avoid the required validation when Button2 is clicked, first set the ValidationGroup property of the RequiredFieldValidator to any non empty value, let's say Submit:
<asp:RequiredFieldValidator ID="rq1" runat="server" ValidationGroup="Submit" />
then the submit button must have the same value for its ValidationGroup property so it will validate the textboxes when it's clicked:
<asp:Button ID="Button1" runat="server" ValidationGroup="Submit" />
finally do not set the ValidationGroup property of the clear button so it will ignore the validation defined in rq1 when it's clicked:
<asp:Button ID="Button2" runat="server" />
For more information regarding ValidationGroup, see here: http://msdn.microsoft.com/en-us/library/ms227424%28v=vs.100%29.aspx

Get current TextBox Text value in a button onClick event - asp.net

I have such a page
<form runat="server" id="NewForm">
Name: <asp:TextBox ID="Name" runat="server"></asp:TextBox>
<asp:Button ID="AddNewName" runat="server" Text="Add" OnClick="AddNewName_Click" />
<asp:Label ID="NewName" runat="server"></asp:Label>
</form>
In the code behind, I have a Page_Load which assign a value to the TextBox Name.
protected void Page_Load(object sender, EventArgs e)
{
Name.Text = "Enter Your Name Here";
}
Then upon the Click on the button AddNewName, I will write it in the Label NewName
protected void AddNewDivision_Click(object sender, EventArgs e)
{
NewName.Text = Name.Text;
}
But, no matter what I input in the Name TextBox, the Label only displays "Enter Your Name Here". It never updates to the actual content in the Name TextBox. What am I doing wrong with this code?
The problem is that you are always overwriting the changed value in Page_Load. Instead, check the IsPostBack property:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
Name.Text = "Enter Your Name Here";
}
You are re-assigning the text to Name every time Page_Load that over writes the text you entered in TextBox before reach AddNewDivision_Click event. To assign it once on page load and do not over write the subsequent calls you can use Page.IsPostBack property.
if(!Page.IsPostBack)
Name.Text = "Enter Your Name Here";
Or you can assign the text in design html and remove the statement from page_load
<asp:TextBox ID="Name" runat="server" Text="Enter Your Name Here"></asp:TextBox>
Another immediately obvious issue is:
<form runat="server" id="NewForm">
Name: <asp:TextBox ID="Name" runat="server"></asp:TextBox>
<asp:Button ID="AddNewName" runat="server" Text="Add" **OnClick="AddNewName_Click"** />
<asp:Label ID="NewName" runat="server"></asp:Label>
</form>
Not the asterisks above. Then, you were wondering why this didn't run:
protected void **AddNewDivision_Click**(object sender, EventArgs e)
{
NewName.Text = Name.Text;
}
Again, note the asterisks. You weren't calling the correct void, in fact you were probably calling a void that didn't even exist.

Why is my repeater control empty on postback?

I think this is a "doh" moment caused by me not having dome WebForms dev for a few years..
I have a repeater which which contains a bunch of checkboxes:
<asp:Repeater EnableViewState="true" ID="IDTypesRepeater" runat="server" OnItemDataBound="IdTypesRepeaterItemDataBound">
<HeaderTemplate/>
<ItemTemplate>
<asp:CheckBox EnableViewState="true" ID="chkIdType" Text="<%# ((KeyValuePair<string,int>)Container.DataItem).Key %>" runat="server" />
<asp:HiddenField ID="idType" Value="<%# ((KeyValuePair<string,int>)Container.DataItem).Value %>" runat="server"/>
<br />
</ItemTemplate>
</asp:Repeater>
I need to get the checkboxes that are selected in the code behind:
foreach (RepeaterItem repeaterItem in IDTypesRepeater.Items)
{
if ( ((CheckBox)repeaterItem.FindControl("chkIdType")).Checked )
{
// Do something
}
}
But on postback, this code isn't working! I know about always databinding a repeater, so I've done this:
protected void Page_Load(object sender, EventArgs e)
{
IDTypesRepeater.DataSource = DocTemplateHelper.GetApplicableIDTypes().Where(type => type.Value != 0);
IDTypesRepeater.DataBind();
}
So this repopulates the repeater, but the Update code never finds any checked checkboxes.. Any ideas?
Bind in the Page_Init event
protected void Page_Init(object sender, EventArgs e)
{
IDTypesRepeater.DataSource = DocTemplateHelper.GetApplicableIDTypes().Where(type => type.Value != 0);
IDTypesRepeater.DataBind();
}
Be sure to use the !Page.IsPostBack method in your pageload.
Otherwise, the Repeater will keep getting reset, and all your checkboxes
will be in there default value (unchecked)
This should fix it. You are binding the control on postback hence losing the values. You can bind it after handling any event to show the updated record.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
IDTypesRepeater.DataSource = DocTemplateHelper.GetApplicableIDTypes().Where(type => type.Value != 0);
IDTypesRepeater.DataBind();
}
}

gridview edit requires to click twice

Why is that I need to click the edit link twice, in a gridview control, before my row enters into edit mode?
<asp:ObjectDataSource ID="ods" runat="server" TypeName="Employee"
SelectMethod="GetAll" ></asp:ObjectDataSource>
<asp:GridView ID="GridView1" runat="server" CssClass="styled"
OnRowCommand="gv_RowCommand" DataSourceID="ods"
OnSorting="gv_Sorting" >
<Columns>
...........
</Columns>
<ItemTemplate>
<ItemTemplate>
<div class='actions'>
<asp:Button ID="btnEdit" runat="server" Text=" Edit " ToolTip="Edit Row" CommandName="Edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"Id") %>' CausesValidation="False" />
<span style="padding-left:10px"></span>
</div>
</ItemTemplate>
</asp:GridView>
protected override void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.ods.SelectParameters[0].DefaultValue = "";
}
}
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == CRUID.Edit.ToString())
{
this.gv.ShowFooter = false;
}
}
You need to avoid rebinding your gridview on each postback.
If not ispostback then
GridView1.DataSource = dt
GridView1.DataBind()
end if
Otherwise you just overwrite Gridview changes.
Great explanation at this link...
http://www.pcreview.co.uk/forums/gridview-two-clicks-needed-enter-place-editing-t3328887.html
Try handling the RowEditing event to set the EditItem Index:
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex
}
There are some mistakes in your code as i examined. Correct your code as shown below:
<asp:ObjectDataSource ID="ods" runat="server" TypeName="Employee"
SelectMethod="GetAll" ></asp:ObjectDataSource>
<asp:GridView ID="GridView1" runat="server" CssClass="styled"
OnRowCommand="gv_RowCommand" DataSourceID="ods"
OnSorting="gv_Sorting" >
<Columns>
...........
<asp:TemplateField>
<ItemTemplate>
<div class='actions'>
<asp:Button ID="btnEdit" runat="server" Text=" Edit " ToolTip="Edit Row" CommandName="Edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"Id") %>' CausesValidation="False" />
<span style="padding-left:10px"></span>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected override void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.ods.SelectParameters[0].DefaultValue = "";
}
}
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
this.gv.ShowFooter = false;
}
}
If on using this code the problem does not solve then there may be some problem in your cssclass which you used with your GridView as I have Checked your code on my machine using ObjectDataSource and it works well using edited code.
Also I want to know that what is CRUID in CRUID.Edit.ToString()
and why you used the following line in Page_Load event
this.ods.SelectParameters[0].DefaultValue = "";
as there are no parameter associated with your SelectMethod="GetAll" method used in ObjectDataSource.
May this answer help you.
I guess there is some conflict with the updatepanels on your page..
Try removing all your Update Panels and try again.. It will work for sure.. Mine worked a few seconds ago.. so thought It would be good to share..

Howto add postbackurl equivalent property to <div runat="server" />

I need a div element to cause postback to the server. I can use window.location tough but then I think I won't be able to fire any event back on the server. How can I call the asp.net generated dopostback function properly? Thx in advance
The idea is that I place a linkButton, and then I use it from the Div
<form id="form1" runat="server">
<div>
<div runat="server" ID="txtTest" style="cursor:pointer;" >Click the div</div>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click" style="display:none;"></asp:LinkButton>
<br /><asp:Literal runat="server" ID="txtDebug"></asp:Literal>
</div>
</form>
and code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtTest.Attributes.Add("onclick", "__doPostBack('"+LinkButton1.ClientID+"','')");
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
txtDebug.Text = "click on div";
}

Resources