I need to display a list of clients, but display them differently based on a parameter.
To do this, I have a gridvew, and inside there is a user control. That control has an "if" based on the type.
My problems:
If I add a button inside the control, when it is pressed I get a button validation error.
If I disable validation errors (enableEventValidation="false"), I get button commands to work, but I'm not able to change values on the control either with full postbacks or an updatepanel.
<asp:GridView ID="gvClients" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<xxx:ClientListGridItem ID="ClientListItem1" runat="server" Client='<%# ((Client) Container.DataItem) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
ClientListGridItem.ascx :
<% if (Client.Style >= 100)
{
%>
<div class="ClientListItem1">
...
<%
}
else
{
%>
<div class="ClientListItem2">
<asp:Button ID="Button2" runat="server" onclick="Button1_Click" Text="Button" />
...
<%
}
%>
I'm sure there is prettier, more object oriented way to do this too...
Changing ClientListGridItem.ascx into:
<asp:Panel id="Div1" CssClass="ClientListItem1" runat="server">
...
</asp:Panel>
<asp:Panel id="Div2" CssClass="ClientListItem2" runat="server">
<asp:Button ID="Button2" runat="server" onclick="Button1_Click" CausesValidation="false" Text="Button" />
..
</asp:Panel>
<script runat="server">
override void OnDataBinding(EventArgs e) {
Div1.Visible = Client.Style >= 100;
Div2.Visible = ! Div1.Visible;
}
</script>
should work.
Related
I need the button inside the datalist to open another page. However the response.redirect does not work !
here is the html
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="Server">
<div id="content">
<!-- Review -->
<div class="products">
<h3>My Books</h3>
<h4>Items you have purchased</h4>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"></asp:SqlDataSource>
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" Width="618px" Height="114px">
<ItemTemplate>
<asp:Label ID="bookid" runat="server" Text='<%# Eval ("BookID") %>' Visible=" false"></asp:Label>
<asp:Label ID="bookname" runat="server" Text='<%# Eval ("Title") %>'></asp:Label>
<asp:Button ID="review" runat="server" Text="Review" CommandName="review" />
</ItemTemplate>
</asp:DataList>
<br />
</div>
</div>
below is the cs file
public void DataList1_ItemCommand(Object source, DataListCommandEventArgs e)
{
if (e.CommandName == "review")
{
DataList1.SelectedIndex = e.Item.ItemIndex;
Label bookid = (Label)DataList1.SelectedItem.FindControl("Bookid");
Response.Redirect("Review.aspx");
}
}
You still need to attach ItemCommand event to DataList1.
<asp:DataList OnItemCommand="DataList1_ItemCommand" ...>
...
</asp:DataList>
Try Server.Transfer("Review.aspx", true);
I have a modal popup that has a gridview, this gridview has a number of rows, I only want the user to be able to select one row. So if they select another it will deselect the previous one.
I have tried a number of methods but can't get the oncheckedchanged event to fire.
Please can someone assist
Cheers
<asp:button id="btnShowPopupOW" style="display: none" runat="server" />
<asp:modalpopupextender id="mpeOW" behaviorid="mpeOW" runat="server" targetcontrolid="btnShowPopupOW"
popupcontrolid="pnlpopupOW" cancelcontrolid="imgOWCancel" backgroundcssclass="modalBackground" />
<asp:panel id="pnlpopupOW" runat="server" width="600px" style="display: none;" class="ModalPanel">
<div style="position: relative; min-height: 490px;">
<asp:UpdatePanel ID="upExisting" runat="server" ChildrenAsTriggers="true">
<ContentTemplate>
<table style="width: 600px;">
<tr height="25px">
<td>
<asp:Panel ID="pnlPrev" runat="server" Height="200px" ScrollBars="Auto" HorizontalAlign="Center">
<asp:GridView ID="grdPrevious" runat="server" ClientIDMode="Static" AutoGenerateColumns="false" Width="100%"
ShowFooter="false" ShowHeaderWhenEmpty="false" DataKeyNames="ID" >
<Columns>
<asp:BoundField DataField="dates" HeaderText="Dates" />
<asp:BoundField DataField="Prev" HeaderText="Previous" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="ChkSelect" runat="server" OnCheckedChanged="ChkSelect_OnCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</td>
</tr>
</table>
</ContentTemplate>
<Triggers>
</Triggers>
</asp:UpdatePanel>
</div>
</asp:panel>
with the following in the codebehind
protected void ChkSelect_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox activeCheckBox = sender as CheckBox;
foreach (GridViewRow rw in grdPrevious.Rows)
{
CheckBox chkBx = (CheckBox)rw.FindControl("ChkSelect");
if (chkBx != activeCheckBox)
{
chkBx.Checked = false;
}
else
{
chkBx.Checked = true;
}
}
}
You can do it like this. With single check box selection using jquery....
<ItemTemplate>
<asp:CheckBox ID="ChkSelect" runat="server" onclick="CheckOne(this)" />
</ItemTemplate>
function CheckOne(obj) {
var grid = obj.parentNode.parentNode.parentNode;
var inputs = grid.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "checkbox") {
if (obj.checked && inputs[i] != obj && inputs[i].checked) {
inputs[i].checked = false;
}
}
}
}
you want to a single checked in check box . i think Using this you will be able to select only one check box at a time. Put the following java script code in the head section of the web page
<script type="text/javascript">
function SingleCheckboxCheck(ob)
{
var gridvalue = ob.parentNode.parentNode.parentNode;
var inputs = gridvalue.getElementsByTagName("input");
for(var i=0;i<inputs.length;i++)
{
if (inputs[i].type =="checkbox")
{
if(ob.checked && inputs[i] != ob && inputs[i].checked)
{
inputs[i].checked = false;
}
}
}
}
</script>
Here checkbox as a TemplateField of the GridView inside just call the above javascript function to make it single checkable.
onclick ="SingleCheckboxCheck(this)"
What you need is probably a (RadioButton)[http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.radiobutton(v=vs.110).aspx] instead of a CheckBox.
You can group the RadioButton's together which then allows the user to only select one item and automatically deselects the previous selected - or: exactly the behavior you want for your application.
This example code is taken from the linked MSDN Documentation. Note the GroupName attribute
<%# Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>RadioButton Example</title>
<script language="C#" runat="server">
void SubmitBtn_Click(Object Sender, EventArgs e) {
if (Radio1.Checked) {
Label1.Text = "You selected " + Radio1.Text;
}
else if (Radio2.Checked) {
Label1.Text = "You selected " + Radio2.Text;
}
else if (Radio3.Checked) {
Label1.Text = "You selected " + Radio3.Text;
}
}
</script>
</head>
<body>
<h3>RadioButton Example</h3>
<form id="form1" runat="server">
<h4>Select the type of installation you want to perform:</h4>
<asp:RadioButton id="Radio1" Text="Typical" Checked="True" GroupName="RadioGroup1" runat="server" />
<br />
This option installs the features most typically used. <i>Requires 1.2 MB disk space.</i><br />
<asp:RadioButton id="Radio2" Text="Compact" GroupName="RadioGroup1" runat="server"/>
<br />
This option installs the minimum files required to run the product. <i>Requires 350 KB disk space.</i>
<br />
<asp:RadioButton id="Radio3" runat="server" Text="Full" GroupName="RadioGroup1" />
<br />
This option installs all features for the product. <i>Requires 4.3 MB disk space.</i>
<br />
<asp:button text="Submit" OnClick="SubmitBtn_Click" runat="server"/>
<asp:Label id="Label1" font-bold="true" runat="server" />
</form>
I have the following gridview and "loadingbar" div:
<asp:GridView ID="importLogGV"
AutoGenerateColumns="false"
DataKeyNames="id"
OnRowDeleting="DeleteStuff"
runat="server"
CssClass="searchListGridView">
<Columns>
<asp:BoundField HeaderText="ID" DataField="id">
[...]
<asp:TemplateField ItemStyle-Width="120">
<ItemTemplate>
<asp:LinkButton ID="lnkDel" CommandName="Delete" Text='Delete' runat="server"
OnClientClick="return confirm('Really delete?');" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<div id="deleting" style="text-align: center" Visible="False" runat="server"
ClientIDMode="Static">Delete in progress...<br />
<asp:Image runat="server" ID="Image1" ImageUrl="~/Images/ajax-loadingbar.gif"/>
</div>
While the eventhandler "DeleteStuff" runs (it does lots of database things than can take up to 10 seconds) in code behind I need to show the "deleting" div, and when the eventhandler finishes it need to hide the div again.
Here's my eventhandler for the delete in codebehind:
protected void DeleteStuff(object sender, GridViewDeleteEventArgs e)
{
deleting.Visible = true;
System.Threading.Thread.Sleep(5000);
deleting.Visible = false;
}
I've tried using style:display instead of the visible attribute, I've tried using a asp:panel instead of a div, but whatever I do, the eventhandler runs but the div is never shown while it runs.
I thought of showing the div with javascript when the user clicks the delete button, but how would I be able to hide it again then after the DeleteStuff process finishes?
Whenever you click the button to delete, a submit request is sent to the server and your page starts to refresh. The page will be re-rendered after the method on the server finishes execution. So whatever you do inside your method, the last effect will retain. If you want to show busy text on the browser, ajax is the way; you will have to use update panels. Try this:
The javascript:
<script type="text/javascript">
function showDeleteConfirm()
{
var isSure = confirm('Really delete?');
if(isSure)
{
document.getElementById("deleting").style['display'] = 'block';
}
}
</script>
aspx markup:
<asp:UpdatePanel id="up1" runat="server">
<ContentTemplate>
<asp:GridView ID="importLogGV"
AutoGenerateColumns="false"
DataKeyNames="id"
OnRowDeleting="DeleteStuff"
runat="server"
CssClass="searchListGridView">
<Columns>
<asp:BoundField HeaderText="ID" DataField="id">
[...]
<asp:TemplateField ItemStyle-Width="120">
<ItemTemplate>
<asp:LinkButton ID="lnkDel" CommandName="Delete" Text='Delete' runat="server"
OnClientClick="return showDeleteConfirm();" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<div id="deleting" style="text-align: center" style="display:none" runat="server"
ClientIDMode="Static">Delete in progress...<br />
<asp:Image runat="server" ID="Image1" ImageUrl="~/Images/ajax-loadingbar.gif"/>
</div>
</ContentTemplate>
</asp:UpdatePanel>
Code behind:
protected void DeleteStuff(object sender, GridViewDeleteEventArgs e)
{
//... Real task here
System.Threading.Thread.Sleep(5000);
deleteing.Style['display'] = 'none';
}
By the way, you need to add a ScriptManager on the page.
Have you tried:
protected void DeleteStuff(object sender, GridViewDeleteEventArgs e)
{
if (CheckAccess())
{
deleting.Visible = true;
System.Threading.Thread.Sleep(5000);
deleting.Visible = false;
}
else
{
Dispatcher.BeginInvoke((Action) (() => DeleteStuff(sender, e)));
}
}
Btw, you should probably use a Timer instead of Thread.Sleep(5000).
In an asp.net application, I have the follow code in the aspx page:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% foreach (var questionPaper in QuestionPapers) { %>
<div style="border-bottom: 1px solid; padding-bottom:20px">
<%= questionPaper.University %><br/>
<%= questionPaper.CourseName %>: <%= questionPaper.CourseCode %><br/>
<%= questionPaper.Type %><br/>
<%= questionPaper.Year %><br/>
<asp:Button ID="View_Paper" runat="server" OnClick="ViewPaper" Text="View Paper"/>
</div>
<% } %>
</asp:Content>
I want to pass questionPaper.ID back to the ViewPaper() event handler on the server side, how do I do that?
public void ViewPaper(object sender, EventArgs e)
{
}
My recommendation is to use a Repeater or a ListView control
Use a Repeater if the data will be used as read-only
Use a ListView if you want to page your results and/or allow the end user to perform CRUD operation over your data
For example, your code would look like this when using a Repeater and an ObjectDataSource controls:
ASPX
<asp:ObjectDataSource ID="ods" runat="server"
SelectMethod="GetQuestionPapers"
TypeName="Your_Namespace.PapersRepository">
</asp:ObjectDataSource>
<asp:Repeater runat="server" DataSourceID="ods" ID="r" OnItemCommand="r_ItemCommand">
<ItemTemplate>
<div style="border-bottom: 1px solid; padding-bottom:20px">
<asp:HiddenField runat="server" ID="paperID" Value='<%# Eval("PaperID") %>'/>
<asp:Label runat="server" ID="university" Text='<%# Eval("University") %>'/><br />
<asp:Label runat="server" ID="courseName" Text='<%# Eval("CourseName") %>'/>:<asp:Label runat="server" ID="courseCode " Text='<%# Eval("CourseCode ") %>'/><br />
<asp:Label runat="server" ID="paperType" Text='<%# Eval("Type") %>' /><br />
<asp:Label runat="server" ID="year" Text='<%# Eval("Year") %>' /><br />
<asp:Button ID="View_Paper" runat="server" Text="View Paper" CommandName="ViewPaperCommand"
/>
</div>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:Repeater>
ASPX code behind
protected void r_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "ViewPaperCommand":
var hidden = e.Item.FindControl("paperID") as HiddenField;
var myPaperID = hidden.Value;
break;
}
}
Papers repository class
namespace Your_Namespace
{
public class PapersRepository
{
public IEnumerable < QuestionPaper > GetQuestionPapers()
{
return QuestionPapers.AsEnumerable();
}
}
}
You could bind it to a control and then reference that control directly?
I know you have multiple questions papers being output, so that in itself is the issue. It's been a while since I have used webforms, but could you not use a repeater control which would give you a row context within which you could get the specific question paper id you are looking for?
Use a repeater. That kind of old skool ASP classic code has no place in ASP.Net projects. Here's an example: http://www.dotnetcurry.com/ShowArticle.aspx?ID=663
It is possible to customise a repeater to make a lightweight DataGrid or ListView equivalent too.
Using: .NET 3.5SP1, VS2008
I was editting someone else asp.net script, he did the Data retreving at the Page_Load while Page is not postback.
I could see the data was populated into the DropDownList properly even after I refresh, navigates, postback in the page.
I added couples more DropDownList and some CheckBoxes into the script, only the DropDownList I added got populated properly. But not the CheckBox.
So I do a test in a new project, which is similar to its script structure:
ASPX:
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
</asp:DropDownList>
<%
if (DropDownList1.SelectedValue == "Item2")
{
%>
<asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox 1" />
<asp:TextBox ID="TextBox1" runat="server" Text="" />
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
</asp:DropDownList>
<%
}
%>
</div>
</form>
Code-Behind:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.CheckBox1.Checked = true;
this.CheckBox1.Text = "Hello CheckBox";
this.TextBox1.Text = "Hello TextBox";
this.DropDownList2.SelectedValue = "Item2";
}
}
}
So as you see the code, when the page first load, the CheckBox1's text will change, Checked will be true, so as other TextBox and DropDownList2
After I select DropDownList1's item to Item2, when the CheckBox1, TextBox1, DropDownList2 nothing got setted, except the CheckBox1.Text.
Why is this happen?
EDIT:
I tried to put them into Panel, in this way it work. But the problem is the program I am editting is using the format above.. So I am not allow to change them all to Panel.
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
</asp:DropDownList>
<%
if (DropDownList1.SelectedValue == "Item2")
{
this.MyPanel.Visible = true;
}
else
{
this.MyPanel.Visible = false;
}
%>
<asp:Panel ID="MyPanel" runat="server" Visible="false" >
<asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox 1" />
<asp:TextBox ID="TextBox1" runat="server" Text="" />
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
</asp:DropDownList>
</asp:Panel>
</div>
</form>
Try out setting EnableViewState and ViewStateMode, perhaps some of the parent controls has disabled it so default Inherit value has been applied.
MSDN:
The default value of the ViewStateMode property for a Web server
control in a page is Inherit. As a result, if you do not set this
property at either the page or the control level, the value of the
EnableViewState property determines view-state behavior.
<asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox 1"
EnableViewState="true"
ViewStateMode="Enabled" />