Currently I have a gridview that when I click on select it populates values in a listview.
I handle this in code behind using the selectedindexchanged event. And I even populate a text box in the insertitem template for new entries.
protected void GridView9_SelectedIndexChanged(object sender, EventArgs e)
{
// this handles the transmittal costs
SqlDataSource29.SelectParameters.Clear();
SqlDataSource29.SelectParameters.Add("tc_t_id", App_id);
SqlDataSource29.InsertParameters.Clear();
ListView1.Visible = true;
ListView1.DataBind();
((TextBox)ListView1.InsertItem.FindControl("tc_t_idTextBox")).Text = App_id;
However the issue arises when I do an insert. I lose value I put into the listviews insertitem texbox tc_t_idTextBox. Actually I lose the value when I edit and delete also.
There must be a way to hold onto that value between inserts.
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert"
Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Clear" />
</td>
<td>
<asp:TextBox ID="tc_dateTextBox" runat="server" Text='<%# Bind("tc_date") %>' />
</td>
<td>
<asp:TextBox ID="tc_costTextBox" runat="server" Text='<%# Bind("tc_cost") %>' />
</td>
<td>
<asp:TextBox ID="tc_typeTextBox" runat="server" Text='<%# Bind("tc_type") %>' />
</td>
<td>
<asp:TextBox ID="tc_commentTextBox" runat="server" Text='<%# Bind("tc_comment") %>' />
</td>
<td>
</td>
<td>
<asp:TextBox ID="tc_t_idTextBox" runat="server"
Text='<%# Bind("tc_t_id") %>' Enabled="false" Width="15" />
</td>
</tr>
</InsertItemTemplate>
By using DataBind() on every PostBack, the ListView is being reloaded every time the user performs and action. In the Page_Load of your page, do this instead:
SqlDataSource29.SelectParameters.Clear();
SqlDataSource29.SelectParameters.Add("tc_t_id", App_id);
SqlDataSource29.InsertParameters.Clear();
ListView1.Visible = true;
if(!IsPostBack)
{
ListView1.DataBind();
}
((TextBox)ListView1.InsertItem.FindControl("tc_t_idTextBox")).Text = App_id;
Related
I have a Listview with a small validation on ItemInserting.
The problem is that it isn't working.
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
</td>
<td> </td>
<td>
<asp:TextBox ID="ProgrammeTextBox" runat="server" Text='<%# Bind("Programme") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required" ControlToValidate="ProgrammeTextBox" Enabled="false"></asp:RequiredFieldValidator><br />
</td>
<td>
<asp:CheckBox ID="OutcomeCheckBox" runat="server" Checked='<%# Bind("Outcome") %>' />
</td>
</tr>
</InsertItemTemplate>
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
CheckBox chk = e.Item.FindControl("OutcomeCheckBox") as CheckBox;
RequiredFieldValidator RFV1 = e.Item.FindControl("RequiredFieldValidator1") as RequiredFieldValidator;
if (chk.Checked)
{
RFV1.Enabled = true;
}
else
{
RFV1.Enabled = false;
}
}
}
The ListView1_ItemInserting isn't applying the Required Field Validator when the checkbox is checked.
Any ideas???
I cant work out why this is not firing.
The Problem is when I check the checkbox it works great, but when I check the checkbox on next page. previous page check boxes gets unchecked.
Checked box Checked/Unchecked event fires CheckedChanged event. But when I check the checkbox in listview next page of listview it uncheck's the checkboxes of listview previous Page.
ListView.aspx Code
<table class=" example1 table table-bordered table-striped">
<thead>
<tr>
<th>Sr no.</th>
<th>Parent Category</th>
<th>Title</th>
<th>Description</th>
<th>Image</th>
<th>Show on Homepage</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<asp:ListView ID="ListCourse" runat="server" OnItemCommand="ListCourse_ItemCommand" DataKeyNames="CID">
<LayoutTemplate>
<tr id="ItemPlaceholder" runat="server">
</tr>
</LayoutTemplate>
<ItemTemplate>
<tr class="gradeA">
<td>
<asp:Label ID="lblSrno" runat="server" Text='<%# Container.DataItemIndex+1 %>'></asp:Label>
</td>
<td>
<asp:Label ID="lbl" runat="server" Text='<%# GetCourse(Convert.ToInt32( Eval("CatID"))) %>'></asp:Label>
</td>
<td>
<asp:Label ID="Lbltitle" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
<asp:Label ID="lablc" runat="server" Visible="false" Text='<%# Eval("CID") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblDescrption" runat="server" Text='<%# (Eval("Description").ToString().Length <=200)?Eval("Description").ToString(): Eval("Description").ToString().Substring(0, 200) + "..."%>'></b></asp:Label>
</td>
<td>
<img class="img_show " src="/Gallery/<%# Eval("Image")%>">
</td>
<td>
<asp:CheckBox ID="CheckCourse" runat="server" Style="margin-left: 50px;" OnCheckedChanged="CheckCourse_CheckedChanged" AutoPostBack="true" />
</td>
<td>
<asp:LinkButton ID="LinkEdit" runat="server" PostBackUrl='<%# "Add_New_Course.aspx?ID="+ Eval("CID")%>'>Edit</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LinkDelete" runat="server" CommandName="DeleteCourse" CommandArgument='<%# Eval("CID") %>' OnClientClick='return confirm("Do you want to delete record ??")'> Delete</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
</tbody>
</table>
Code Behind CheckedChanged
protected void CheckCourse_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkhome = (CheckBox)sender;
ListViewItem item = (ListViewItem)checkhome.NamingContainer;
ListViewDataItem dataItem = (ListViewDataItem)item;
string code = ListCourse.DataKeys[dataItem.DisplayIndex].Value.ToString();
int CID = Convert.ToInt32(code);
Course_Master objnew = DB.Course_Master.Single(p => p.CID == CID);
bool IsHome = CheckOnHome(CID);
if (IsHome == true)
{
if (checkhome.Checked == false)
{
objnew.ShowOnHomePage = false;
}
}
else
{
if (checkhome.Checked == true)
{
objnew.ShowOnHomePage = true;
}
}
DB.SaveChanges();
}
It doesn't fire because when the postback fires the server doesn't know the previous state of the checkbox, so it doesn't know if it's changed or not.
Try to set the default value to false and it should work
<asp:CheckBox ID="CheckCourse" runat="server" Checked="false" Style="margin-left: 50px;" OnCheckedChanged="CheckCourse_CheckedChanged" AutoPostBack="true" />
You need to save ids somewhere of checked item from the list. As when you move to the page 2 of ListView it lost its previous state.
Store data in viewstate and load it from there. To read and to bind it, you would need to handle PagePropertiesChanging and ItemDataBound event handlers of ListView.
Here is a good explaination regarding Maintaining the state of checkboxes in ListView
Please give +1 if it helped. Cheers!
Thank you Guys for the help. I solved it using some simple procedure's by removing the check-boxes from the ListView and adding text instead of it regarding whether the checkbox is checked or not(Yes/No). As I thought it will the easiest way to solve my problem.
I have a list of objects called dashboard which holds a list of Announcements with their AnnouncementId. My DataListAnnouncements.DataSource is equal to the dashboard but I can't figure out how to get the selectedItem equal to the item that I am selecting in the client side.
What Currently is happening is when I select an unread announcement, it displays the message with some css, but it does that to the wrong announcement at the moment.
protected void DataListAnnouncements_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Select")
{
IList<DashBoardView> dashboard = new List<DashBoardView>();
dashboard = (IList<DataObjects.DashBoardView>)ListOfObjects;
UnreadAnnouncement unread = UnreadAnnouncements;
if (e.CommandArgument != null)
{
if (unread != null)
{
unread.WasRead = true;
UpdateUnreadAnnouncement(unread);
att = unread.AnnouncementId;
}
}
dashboard[e.Item.ItemIndex].WasRead = unread.WasRead;
DataListAnnouncements.DataSource = Session["dashboard"];
DataListAnnouncements.SelectedItem = dashboard[e.Item.ItemIndex]
(DataListAnnouncements.SelectedItem = att <---- Something Like this is what I would like to implement?)
Session["dashboard"] = dashboard;
bindDataList();
}
}
as you see now, I have it using the dashboard[e.Item.ItemIndex].WasRead = unread.WasRead line, but this proves to not work since the index is not always what I want. So I want to use an attribute. I looked around a bit but I am still fuzzy on how to implement it on the C# and asp side.
and this is my ASP.
<ItemTemplate>
<table width="880px">
<tr>
<td class="leftCol">
<asp:LinkButton Text='<%# Eval("title") %>' CssClass="bold" runat="server" CommandName="Select" CommandArgument='<%# Eval("UnreadAnnouncementId") %>' ID="titleLabel" /></span>
</td>
<td class="created">
<asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "Effective", "{0:MM/dd/yyyy}") %>' CssClass="created" runat="server" ID="Label4" />
<br />
<asp:Label Text='<%# Eval("FirstName") %>' runat="server" ID="Label2" />
<asp:Label Text='<%# Eval("LastName") %>' runat="server" ID="Label3" />
</td>
</tr>
<tr>
<td class="one-long-line">
<asp:Label Text='<%# Eval("details") %>' CssClass="details" runat="server" ID="detailsLabel" />
</td>
</tr>
</table>
</ItemTemplate>
<SelectedItemTemplate>
<table width="880px" cellpadding="10px">
<tr>
<td class="leftCol">
<asp:Label Text='<%# Eval("title") %>' CssClass="bold" runat="server" ID="titleLabel" />
<asp:Label runat="server" ID="wasRead" Text='<%# Eval("wasRead") %>' Visible="false" Enabled="false" />
<td class="created">
<asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "Effective", "{0:MM/dd/yyyy}") %>' CssClass="created" runat="server" ID="Label4" />
<br />
<asp:Label Text='<%# Eval("FirstName") %>' runat="server" ID="Label2" />
<asp:Label Text='<%# Eval("LastName") %>' runat="server" ID="Label3" />
</td>
</tr>
<tr>
<td class="deailsTD" colspan="2">
<asp:Label Text='<%# Eval("details") %>' CssClass="details" runat="server" ID="detailsLabel" />
</td>
</tr>
<tr>
</tr>
</table>
</SelectedItemTemplate>
I want to be able to click on a item in the datagrid, and it will expand that item and show me the details.
You have to use `SelectedIndex', not SelectedItem'. Source: How to: Allow Users to Select Items in DataList Web Server Controls.
It could be like this:
DataListAnnouncements.SelectedIndex = e.Item.ItemIndex;
From your code it is not clear what is the relation between dashboard and att. If there's any way to find the att in dashboard and find it's position, you can do like this:
DataListAnnouncements.SelectedIndex = attposition;
EDIT : There's an easy way to control background color of DataLists Items. We can parse each item in DataList's ItemBound and set BackColor like below:
protected void DataListAnnouncements_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem||
e.Item.ItemType == ListItemType.SelectedItem
)
{
// Here BackColor - Grey: WasRead; Yellow: Unread
var wasRead = ((DashBoardView)e.Item.DataItem).WasRead;
e.Item.BackColor = wasRead? System.Drawing.Color.Gray: System.Drawing.Color.Yellow;
}
}
You can even expand it more by splitting the if condition into two:
if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem){}
and
if ( e.Item.ItemType == ListItemType.SelectedItem){}
It seems I have had a lot of questions today.
What i want to do is save the text for each image.
I'm getting the right text for each picture into the textboxes at the moment.
The code behind:
var car = GarageBLL.LoadCar(Convert.ToInt32(CarId),
Convert.ToInt32(_memberId)); ImageRepeater.DataSource = car.Images;
ImageRepeater.DataBind();
protected void FinalizeNewCar(object sender, EventArgs e) {
Response.Redirect("/amcargarasjen"); }
**Code here for saving each edit into the right ImageId.**
ASP:
<asp:Repeater runat="server" ID="ImageRepeater">
<ItemTemplate>
<table>
<tr>
<td>
<a class="deleteLink" href="#" rel="<%#Eval("ImageId")%>" title="">
<asp:Image runat="server" ImageUrl="/Content/Images/Garage/DeleteButton.png" /></a>
<asp:Image Width="60" Height="45" ID="ImgCar" ImageUrl='<%# String.Format("/garageimages/{0}/{1}.{2}", CarId, Eval("ImageId"), Eval("Extension")) %>' runat="server" />
<asp:TextBox runat="server" Text='<%# Eval("Description") %>' ID="txtText"></asp:TextBox>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<br />
<asp:Button ID="Button1" runat="server" Text="Fullfør" OnClick="FinalizeNewCar" />
Anyone got any ideas on how to do that?
Modify your Repeater HTML markup like below. Note the addition of a Hidden field to keep the reference of the current image ID.
<asp:Repeater runat="server" ID="ImageRepeater">
<ItemTemplate>
<table>
<tr>
<td>
<a class="deleteLink" href="#" rel="<%#Eval("ImageId")%>" title="">
<asp:Image runat="server" ImageUrl="/Content/Images/Garage/DeleteButton.png" /></a>
<asp:Image Width="60" Height="45" ID="ImgCar" ImageUrl='<%# String.Format("/garageimages/{0}/{1}.{2}", CarId, Eval("ImageId"), Eval("Extension")) %>' runat="server" />
<asp:TextBox runat="server" Text='<%# Eval("Description") %>' ID="txtText"></asp:TextBox>
<asp:HiddenField runat="server" Value='<%# Eval("ImageId") %>' ID="txtImageId"></asp:TextBox>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<br />
<asp:Button ID="Button1" runat="server" Text="Fullfør" OnClick="FinalizeNewCar" />
CODE FinalizeNewCar Event Handler
protected void FinalizeNewCar(object sender, EventArgs e)
{
foreach (RepeaterItem item in ImageRepeater.Items)
{
Int32 imageId = Convert.ToInt32(((HiddenField) item.FindControl("txtImageId")).Value);
string description = ((TextBox)item.FindControl("txtText")).Text;
//You will get the imageId description here.
//Write your code to update the datatbase.
}
}
"I have a ListView with items to edit and an insert.
The insert item template has several text boxes however I would like to make one of the textboxes readonly and keep a value in it that will persist after every insert.
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
</td>
<td>
<asp:TextBox ID="tc_dateTextBox" runat="server" Text='<%# Bind("tc_date") %>' />
</td>
<td>
<asp:TextBox ID="tc_costTextBox" runat="server" Text='<%# Bind("tc_cost") %>' />
</td>
<td>
<asp:TextBox ID="tc_typeTextBox" runat="server" Text='<%# Bind("tc_type") %>' />
</td>
<td>
<asp:TextBox ID="tc_commentTextBox" runat="server" Text='<%# Bind("tc_comment") %>' />
</td>
<td> </td>
<td>
<asp:TextBox ID="tc_t_idTextBox" runat="server" Text='<%# Bind("tc_t_id") %>' Width="15" ReadOnly="true" />
</td>
</tr>
</InsertItemTemplate>
tc_t_idTextBox is the one that I made readonly and would like that to be the box that keeps the same value on every insert.
In order to make a TextBox readonly you will need to set the ReadOnly=true
example:
<asp:TextBox ReadOnly="True"...
I finally figured out how to fix my issue.
My work around was to add a hidden field to the page.
<asp:HiddenField ID="h_tc_t_id_holder" Value="0" runat="server" />
in code behind I set the value to the hidden field.
h_tc_t_id_holder.Value = App_id;
In the list view i add
OnPreRender="ListView1_OnPreRender"
And in codebehind
protected void ListView1_OnPreRender(object sender, EventArgs e)
{
ListView1.DataBind();
((TextBox)ListView1.InsertItem.FindControl("tc_t_idTextBox")).Text = h_tc_t_id_holder.Value;
}