I have a repeater containing, amongst others, two buttons. One of the buttons is an ASP.NET button whilst the other is of the type "input type="button"".
Now, in my code-behind, I want to retrieve both buttons from the repeater to either hide them or show them. I have successfully hidden the ASP.NET button however I do not know how to retrieve the other button.
Here is some code in ASP.NET:
<input type="button" name="ButtonEditUpdate" runat="server" value="Edit Update" class="ButtonEditUpdate" />
<asp:Button ID="ButtonDeleteUpdate" CssClass="ButtonDeleteUpdate" CommandName="Delete" runat="server" Text="Delete Update" />
Here is the code-behind:
protected void RepeaterUpdates_ItemBinding(object source, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
TextBox Update_ID = (TextBox)item.FindControl("TextBoxUpdateID_Repeater");
//Button Edit_Update = (Button)item.FindControl("ButtonEditUpdate");
Button Delete_Update = (Button)item.FindControl("ButtonDeleteUpdate");
if (Social_ID == String.Empty)
{
//Edit_Update.Visible = false;
Delete_Update.Visible = false;
}
}
How can I retrieve the HTML button and hide it since it is NOT an ASP.NET button?
That button is a HTML control and will be of type System.Web.UI.HtmlControls.HtmlButton
System.Web.UI.HtmlControls.HtmlButton button = item.FindControl("ButtonEditUpdate") as System.Web.UI.HtmlControls.HtmlButton;
if(button!=null)
button.Visible = false;
In general you cannot straightly retrieve a plain-old HTML button because ASP.NET considers that as part of the text markup. Fortunately, you already added runat="server" that makes your button a server control.
The easiest way is to use an HtmlButton control. But in your markup you need the id attribute
<input type="button" name="ButtonEditUpdate" runat="server" value="Edit Update" class="ButtonEditUpdate" id="ButtonEditUpdate" />
Then in the code behind
//Button Edit_Update = (Button)item.FindControl("ButtonEditUpdate");
HtmlButton Delete_Update = (HtmlButton)item.FindControl("ButtonEditUpdate");
If you just want to set the visibility you shouldn't need to cast it.
var myButton = e.Item.FindControl("ButtonEditUpdate");
if(myButton != null)
myButton.Visible = false;
EDIT: you should give your button an ID.
Related
I'm not really proficient in asp.net. I Have a asp based web application and I want to create a custom GridView in order to use it whenever I have a search box and reduce redundancy in my code.
I want to have this GridView below my textbox and on text changing the GridView shows mostly searched results and a "More" button for advance search which will open a new page. Can anybody help me how can I start?
Thanks.
Here a small example of how you could achieve this. First add the necessary items needed to do a search to the aspx page. Note that the buttons have an OnCommand so that you can send a CommandName along with them.
<asp:TextBox ID="SearchField" runat="server" MaxLength="50"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="A search term is required" ValidationGroup="Search"
ControlToValidate="SearchField">
</asp:RequiredFieldValidator>
<asp:Button ID="SearchButton" runat="server" Text="Search"
OnCommand="DoSearch_Command" CommandName="Search" ValidationGroup="Search" />
<asp:GridView ID="SearchResultsGridView" runat="server" AutoGenerateColumns="true"></asp:GridView>
<asp:Button ID="MoreButton" runat="server" Text="More"
OnCommand="DoSearch_Command" CommandName="More"/>
Now in the code behind you process the button clicks of Search and More. I created some dummy data with a List, but you need to replace that with the correct data source that holds your search results (List, DataTable etc).
protected void DoSearch_Command(object sender, CommandEventArgs e)
{
//create a new item to hold search results, in this case a list
List<string> searchResults = new List<string>();
//the text from the textbox that contains the search word
string searchTerm = SearchField.Text.Trim();
//hide the 'more' button
MoreButton.Visible = false;
//add some dummy data for testing
for (int i = 1; i <= 50; i++)
{
searchResults.Add("Search result " + i);
}
//if the results are more than 10 and the click is not from the 'more' button take 10 items
if (searchResults.Count > 10 && e.CommandName == "Search")
{
searchResults = searchResults.Take(10).ToList();
//show the more button
MoreButton.Visible = true;
}
//show results in gridview
SearchResultsGridView.DataSource = searchResults;
SearchResultsGridView.DataBind();
}
enter code here NET application, I have inserted a button that call a Javascript function (OnClick event) and a asp.net function (OnClick event)
The problem is that when I click the button, it refreshes the page.
How can I avoid the page refreshes click on asp button using javascript?
document.getElementById('pageurl').innerHTML = "tryfblike.aspx";
$('#<%= btnsave.ClientID %>').click();
$('#auth-loggedout').hide();
<asp:Button runat="server" ID="btnsave" OnClick="btnsave_Click();" Visible="true" style="display: none;" />
protected void btnsave_Click(object sender, EventArgs e)
{
objda.agentid = "2";
string currenttime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
objda.datetime = currenttime;
ds = objda.tbl_log();
}
First to call JavaScript function, use OnClientClick.
To avoid page refresh, after your function call add return false;
For example:
<asp:Button ID="btnSubmit" runat="Server" OnClientClick="btnsave_Click(); return false;" />
in java script you can use return false.
if you want prevent whole page referesh use ajax.
FirstYou can call javascrip onclick and then simply add
return false;
I have a repeater which displays and data bind the source of tab links. Here is the code:
protected void rptTab_ItemBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Item i = e.Item.DataItem as Item;
Link hlTabLink = e.Item.FindControl("hlTabLink") as Link;
hlTabLink.Target = Sitecore.Links.LinkManager.GetItemUrl(i);
hlTabLink.DataSource = i.Paths.FullPath;
hlTabLink.Field = "Title";
}
}
Now this is my markup:
<asp:Repeater ID="rptTab" runat="server" OnItemDataBound="rptTab_ItemBound">
<ItemTemplate>
<li id= "liTabTest" runat = "server">
<a>
<sc:Link ID = "hlTabLink" Field = "scTabLink" onclick = "TabClick()" runat ="server"></sc:Link>
</a>
</li>
</ItemTemplate>
</asp:Repeater>
And this is the jQuery which is adding CSS class dynamically based on which item is selected:
$(document).ready(function () {
init();
});
function init() {
$("ul#Tab-labels li").removeClass("tab-label TabbedPanelsTabSelected");
$("ul#Tab-labels li:first").addClass("tab-label TabbedPanelsTabSelected");
};
function TabClick() {
alert('test');
};
Now you can see that I am displaying the CSS file based on the index of the tab. I also have to add the click event in that jQuery. So there are basically two things that I am confused:
My click event in jQuery is not getting called :(
From jQuery I have to know in the click event that which page it has
to go through? So that I have to get from codebehind or what? Like a
hidden field and store the tab pages in that, then fetch out from
jQuery.
How should I resolve this? Please help!
For sc:Link, change the bind method to onClientClick for onclick = "TabClick()". OnClick is for the postback server method.
BTW, what is the sc prefix? Is there a user control you are adding to the page?
I hope I can explain this properly.
I have a grid that is part of our 3rd party shopping cart software. This grid has a row of quantity text boxes into which the customer enters how many of each thing they want to buy.
I put this grid inside a panel so I can set it on or off with
myPanel.Visible=true;
I also have a button to show and one to hide using the above code method.
If I enter a value into a textbox and then click the hide button and then click the show button, when the panel reappears the values are zero. If I then reload the page (browser reload) then the value returns as it was originally. It's a pretty good magic trick but not what I need. What am I doing wrong?
Eventually I want to select a date from a calendar while it is hidden but that is not in play yet... just the show/hide buttons.
Thanks
This sounds like the correct behaviour of ASP.NET WebForms and ViewState
First page load: Panel is visible and the panel loaded with its initial values.
Hide Panel: As soon as the button is pressed a post back occurs. myPanel is set to invisible which on the server side means that the HTML for the panel is not generated (this can be confirmed by looking the generated HTML).
Show Panel: A post back occurs again. But because the values were not rendered in the previous step, they are not available in the ViewState to repopulate the panel.
Reload of the page: This start the process over again (Same as step 1)
A possible solution is to instead hide the panel (<div) on the client-side. This will also have the benefit of not making a round trip to the server to just enable/disable the panel.
Your code should be like below...
Show and Hide button definition to show/Hide the panel is in Java script. That mans there is no handler of Button click event at server side...This approach is suggested normally and fast..
Sample ASPX Code
<script type="text/javascript" language="javascript">
function Hide() {
var ID = document.getElementById('pnl');
ID.style.display = 'none';
return false;
}
function Show() {
var ID = document.getElementById('btnHide');
ID.style.display = 'block';
return false;
}
</script>
<asp:panel id="pnl" runat="server">
<asp:GridView ID="grd" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="ed" runat="server" Text='<%#Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:panel>
<asp:button text="Hide" runat="server" id="Button1" onclientclick="return Hide();" />
<asp:button text="Show" runat="server" id="btnShow" onclientclick="return Show();" />
Sample Code Behind
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
using (DataTable Dt = new DataTable())
{
using (DataColumn Dc = new DataColumn("Name"))
{
Dt.Columns.Add(Dc);
DataRow dr = Dt.NewRow();
dr["name"] = "1";
Dt.Rows.Add(dr);
dr = Dt.NewRow();
dr["name"] = "2";
Dt.Rows.Add(dr);
grd.DataSource = Dt;
grd.DataBind();
}
}
}
}
}
I've been trying to get this working for a couple of hours now but nothing from google could help me fix the problem.
I have a very simple repeater control:
<asp:Panel ID="userDefDiv" Visible="false" runat="server">
<asp:Repeater ID="userDefRepeater" EnableViewstate="false" runat="server">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" EnableViewState="false"></asp:TextBox><br/>
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
the userDefDiv panel is inside another panel, which is inside contentPLaceHolder.
the parent panel to userDefDiv does NOT have the "enableviewstate="false"".
So.
Everything on this page happens after a couple of linkbuttons_click. so nothing happens during page_load. And after i click another linkbutton i want to get the data from the different textboxes that is within the repeater.
C# code:
This is the code to create all the repeater items.
public void createUserDef()
{
DataTable userDefData;
userDefData = ..... (data from Database.)
userDefDiv.Visible = true;
userDefRepeater.DataSource = userDefData;
userDefRepeater.DataBind();
}
The code for the linkbutton:
protected void linkButton_Click(object sender, EventArgs e)
{
createUserDef();
Label2.Visible = true;
foreach (RepeaterItem item in userDefRepeater.Items)
{
TextBox box = (TextBox)item.FindControl("TextBox1");
string b = box.Text;
Label2.Text += b + " . ";
}
}
As you see i create the repeater once again during the click. But the only thing i can read in label2. is a a number of " .", on dot for each textbox.
but the text from the textbox is empty..
What am I doing wrong??
thanks for reading!
Mattias
SOLUTION:
add EnableVIewState="true" to textbox & repeater.
Dont call call dataBind() before you get the values.
Thanks!
You need to set EnableViewState to 'true' for linkbuttons to work properly in a repeater