I need some help with the following.
I have a list view that I fill with code-behind:
Linq->Sql:
ListView1.DataSource = from x in database.ITEMS
select x;
ListView1.DataBind();
Content of ITEMS:
ID
Name
Quantity
I only show the name of the fields:
<tr>
<td>
<asp:Label ID="nameLabel" runat="server" Text='<%# Eval("Name") %>' />
</td>
</tr>
Assuming I have 25 rows in the database, I fill the list view with 25 rows. Next is what I need help with. The user needs to click one of the 25 names (complete row) and redirects to a page where the content of that row is showed. So lets say someone clicks on "Cars", he'll be redirected and he will see: You have selected Cars, Cars has an ID of 19 and a Quantity of 6.
I have browsed the Internet already but I am having trouble finding solutions that I can understand. I am kind of a noob when it comes to list views and other data displaying methods.
My 2 questions:
How do I start a event when I click a row (and not a button or a link in that row)?
When I click a row and start a event, how can I check which one was clicked?
I hope someone can explain to me how this is done in a user friendly noob kind of way or redirect me to a tutorial discussing my issue's and also in a user friendly way. Thanks in advance.
Ok - I am not sure if this is the best way to go with this (I'll be interested what other think)... but this will work .
Update your listview to something like this:
<script type="text/javascript">
// simple redirect to your detail page, passing the selected ID
function redir(id) {
window.location.href = "mydetailpage.aspx?id=" + id;
}
</script>
<table>
<asp:ListView ID="ListView1" runat="server">
<ItemTemplate>
<tr onclick="redir('<%# Eval("ID") %>');">
<td>
<asp:Label ID="nameLabel" runat="server" Text='<%# Eval("Name") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
</table>
Then create another page to show your detail (e.g. mydetailpage.aspx) - this should requery your database and show the other fields, given the id passed in the querystring.
you can define onclick attribute to call javascript function on tr element like this :
onclick="yourfunction('<%# Eval("Name") %>');"
Then you can put anything in your javascript function like redirect to another page
Related
I have a master page that looks something like this:
<table>
<tr>
<td>
<asp:Label ID="lbl1" runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="lbl2" runat="server"></asp:Label>
</td>
</tr>
</table>
I use this design in all pages except one where i would like to add a button right after the first label - lbl1 (both of them to be on same row), and on the second row to see the second label.
My question is how could I achieve this from a content page (from code-behind preferable)?
Create <asp:Button runat="server" Visible="false" ... /> which by default have Visible = false. In the page which you want to see the button find the button and make it Visible = true.
When the Control is not visible it is not render, this is equal to not existing for other pages !
This is so hard to understand, I'll try to describe my problem so clearly.
I have a Table in SQL DB:
COMMENT(idcom, content,user,idtopic);
I use a Repeater Control to display all the comment in Database:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table>
<tr>
<td>
</td>
<td>
<asp:Label ID="lblcontent" runat="server" Text='<%#Eval("content") %>'>
</asp:Label>
</td>
<td>
<asp:Label ID="lbluser" runat="server" Text='<%#Eval("user") %>'>
</asp:Label>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
My problem is: When client add a new comment (in another page)--> insert a new row into COMMENT table---> I have to load all the comment into Repeater again (both old comments and new comments).---> therefor, I use <UpdatePanel> and <Timer Control> Ajax Toolkit to update every 10sec. It's so amateur and not a good solution.
How can I just only load the new comment (new row) into the Repeater in every 10sec.
I always think Repeater is fixed and we cannot insert the single row.
Help! Can you give me some advice to insert single row into Repeater by load data from Database (not input directly)???
I'd say the best way to do this is with some Javascript/jQuery plugin + AJAX queries. ASP.NET Webforms does not usually works that way (it is prefered from the webform point of view to go through the entire page lifecycle for each update).
When you first populate your repeater, store the List and the biggest IdComm in variables, So you will use it in all subsequent selects you do in Database again.
Use the IdComm stored as a parameter and get all the new comments with IdComm bigger than the IDComm you have passed as parameter.
After that, add this new lines in the first list you have requested in the first request and populate the Repeater again in this updatePanel you are using.
Is it possible to programatically insert C# Inline Expressions as the values for ASP.NET Controls in your server-side code?
I'm currently using a DataList to display a lot of data in a table. I want to be able to dynamically change the columns of that table, so I need to be able to edit what controls are in its ItemTemplate.
However, in addition to editing the controls in the ItemTemplate, I need to be able to alter the value that is binded to each control in the template, because I want to dynamically change what is being displayed.
So what I currently have is a static table that doesn't change:
<asp:DataList ID="dataList" runat="server" OnSelectedIndexChanged="dataList_OnSelectedIndexChanged" DataSourceID="peopleData">
<ItemTemplate>
<table>
<tr>
<td>
<asp:LinkButton ID="NameLink" OnClientClick="onPageUpdate()" CommandName="Select" Text='<%# Eval(this.Name) %>' ForeColor='<%# Eval("this.NameColor") %>' runat=Server" />
</td>
<td>
<asp:Label Text='<%# Eval("this.Values[\"Age\"]") %>' ForeColor='<%# Eval("this.ValueColors[\"Age\"]") %>' runat="Server">
</td>
// OTHER COLUMNS WITH DIFFERENT DATA
</tr>
</table>
</ItemTemplate>
</asp:DataList>
// OBJECT DATA SOURCE CODE
I know how to dynamically add Controls to the ASPX web page. However, I don't know how to add the inline expressions.
I've tried doing the following but it doesn't work because of the type mismatches:
Label label = new Label();
label.Text = "<%# Eval(\"this.Values[\\\"Age\\\"]\") %>";
label.ForeColor = "<%# Eval(\"this.ValueColors[\\\"Age\\\"]\") %>";
Is there a way of achieving this or doing something similar?
My only other option that I can think of right now is to scrap using the DataList and ItemTemplate and just generate each row myself.. That's a lot more coding versus just using the ItemTemplate.
Not sure if this would help you, but look at http://msdn.microsoft.com/en-us/library/system.web.ui.databinder(v=vs.100).aspx
It is showing using of the hand-written exposure of properties. If your property were always call "magic" and you return the appropriate value for magic within your code would that get you what you need?
I am developing an application for my company in which i am stuck at thinking what would be the best approach for doing this.
The company already have a page for survey around 15 questions with multiple choices. Now i have been asked to develop an application so that from admin section they can modify the existing questions and their options, also they can add more questions and options.
I am not sure what would be the best approach for doing this. I was thinking creating a page in admin section where they can see existing questions and answer options, if they modify it i can directly change it in the database and if they add more questions, I can go and create a custom questions fields in the database. For answers option i will not create any fields in the database as i will generate options dynamically like textboxes, checkboxes, radiobuttons etc.. and when user select the answer i will put pnly selected answers in the database.
EDIT
=======
I just want to give admin only 3 options for adding options ( checkbox, radios, textbox )
I am confused as how to create controls dynamically using generics. like admin can pass in the values in the method like this CreateControls(TextBox, NoOfOptions, List)
Please forgive me for not adding any code in my questions as i have not yet started working on this and stuck at the first stage of thinking process.
I am using VS2010, .Net 4.0, SQL server 208 R2 for this application.
Please advice. any working example Link would be appreciated.
I have one idea regarding this.I already done this kind of functionality but in my case each custom question is bind with textbox only.
In you case we can follow these steps :
Admin End :
Specify each control with unique value it may be number or control name.Save this in table with question id.
Client End :
1.) Take repeater.In item template take following controls :
a.)Label (Your custom question)
b.)textboxes (visible false)
c.)checkboxes (visible false)
d.)radiobuttons (visible false) etc.
2.)Bind this repeater with questions table.
3.)Now fire rowdatabound event and set visibility of controls on the basis of question id.
May this help you if you have any doubt please ask .
UPDATE :
Now please check the implementation of whole process which i had discussed yesterday :
XML file use as test database :
<?xml version="1.0" encoding="iso-8859-1"?>
<catalog>
<cd>
<id>1</id>
<title>Question 1</title>
<ctrl>1</ctrl>
</cd>
<cd>
<id>2</id>
<title>Question change</title>
<ctrl>2</ctrl>
</cd>
<cd>
<id>3</id>
<title>Question 3</title>
<ctrl>3</ctrl>
</cd>
<cd>
<id>4</id>
<title>Question 4</title>
<ctrl>2</ctrl>
</cd>
ASPX page :
<asp:Repeater ID="Repeater1" runat="server"
onitemdatabound="Repeater1_ItemDataBound">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("id") %>'></asp:Label>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Eval("ctrl") %>' />
</td>
<td>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("title") %>'></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Visible="false"></asp:TextBox>
</td>
<td>
<asp:RadioButton ID="RadioButton1" runat="server" Checked="true" Text="YES" TextAlign="Left" Visible="false" />
<asp:RadioButton ID="RadioButton2" runat="server" Text="NO" TextAlign="Left" Visible="false" />
</td>
<td>
<asp:CheckBox ID="CheckBox1" runat="server" Visible="false" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
CS file :
if (!IsPostBack)
{
DataSet ds = new DataSet();
ds.ReadXml(MapPath("XMLFile.xml"));
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
HiddenField ctrl = (HiddenField)e.Item.FindControl("HiddenField1");
if (ctrl.Value == "1")//1 for Textbox
{
TextBox txtCtrl = (TextBox)e.Item.FindControl("TextBox1");
txtCtrl.Visible = true;
}
else if (ctrl.Value == "3")//3 for Radio Button
{
RadioButton rdbYCtrl = (RadioButton)e.Item.FindControl("RadioButton1");
RadioButton rdbNCtrl = (RadioButton)e.Item.FindControl("RadioButton2");
rdbYCtrl.Visible = true;
rdbNCtrl.Visible = true;
}
else if(ctrl.Value == "2")//2 for Chackbox
{
CheckBox chkCtrl = (CheckBox)e.Item.FindControl("CheckBox1");
chkCtrl.Visible = true;
}
}
}
Hope this help you now :-)
Prettiest way to do this is to write a webcontrol. PLace it inside the ItemTemplate of teh Repeater. The repeater will do the databinding job, where as the webcontrol will render the question and the answer fields for each questions.
What I can imagine at the momment is, your datasource (that is bound to ther repeater must have at least:
QuestionID - This is to identify the question
Question - this contains the question
ResponseType - this is just a flag to determine whether to render textbox, radio or checkbox as the answer field/s
The webcontrol must have at least these properties:
QuestionID
Question
ResponseType
UserAnswer
Choices - collection of choice
During itemdatabound event of the repeater, get the webcontrol instance in the itemtemplate. assign the questionid and question from the bound DataItem to the respective webcontrol properties. Then check the AnswerType. if it's either radio or checkbox, retrieve the choices from the db/xml for this particular questionid, and assign it to the webcontrol Choices property.
Now at the webcontrol's RenderContents event:
write the question
if AnswerType is textbox, write textarea or textbox element
if AnswerType is either radio or checkbox, iterate the Choices property to render the respective input tags.
Anyway, if you use this approach, the admin can set unlimited number of choices for each questions. Each question can have flexibility. Image can be attached to questions, even as the choices like those IQ test.
However, there are a lot more to code in the webcontrol like, maintaining data on postbacks, fieldvalidation, etc.
I have list view with multiple lines and in every line I have textbox where is number of items in cart and refresh button. So if someone change number of items and click on update, I wanna change it in cookie and db. Im using CommandArgument where 1 parameters is ID of row in table between tableProduct and tableCart and second i wanna sent current value of textbox, where is how many items user wanna buy, but i dont know how to add. Can u help me?
<asp:TextBox runat="server" id="countInCart" Text='<%# Eval("countInCart") %>' /><asp:ImageButton ID="ImageButton1" runat="server" ImageAlign="AbsBottom" ImageUrl="~/Static/Img/inp-order-refresh.png" OnCommand="updateClick" CommandArgument='<%# Eval("id") + ";" + **here i want add value of countInCart, but I dont know how** %>' />
you are looking for a way to update them in bulk. have a look at this; http://geekswithblogs.net/thanigai/archive/2009/05/21/bulk-insert-update-and-delete-with-asp.net-listview-control.aspx