How to make GridView take me to a different page - c#

I;m new on SO so please be gentle. I searched and didn't find what I was looking for.
My problem is as follows - I am making a game for Windows Store where you must finish sentences based on the animation you see below the sentence. The game will offer many stories and this is my problem. I used the default GridView template to create the MENU for picking your stories. And this is where I am stuck:
How to tell the app using json that when clicked it should open a specific page (the wanted level)?
This is the code that I need to change probably:
void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
// Navigate to the appropriate destination page, configuring the new page
// by passing required information as a navigation parameter
var itemId = ((SampleDataItem)e.ClickedItem).UniqueId;
this.Frame.Navigate(typeof(ItemDetailPage), itemId);
}
I know that I need to change the last line so that it takes some information from the json file and changes it for a specific page file name from the solution.
I hope that you understand what I mean :(

Use the template field to add a button with your routing argument.
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text="Action" CommandName="Action"
CommandArgument='<%#Bind("ID ") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Code Behind:
protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Action")
{
string ID = e.CommandArgument; // Do something with it.
}

Related

ASP.NET viewstate update without javascript

Hy,
how to update my viewstate on model variable value changed? To make it more clear lets take an example.
I have 3 variables in my model. One of them is bool and other two are strings. when I run my application I have a form of one checkbox. When I click on checkbox I want those two string values appear as a input values and when I click on the checkbox again I want to make them disappear again.
How could I make this happen without javascript?
when Using simple web form application just use
on aspx page
<asp:CheckBox Text="Check" Checked="false" ID="MyCheckBox" runat="server" AutoPostBack="true" OnCheckedChanged="MyCheckBox_CheckedChanged" />
on aspx.cs page
protected void MyCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (MyCheckBox.Checked)
{
Response.Write("CheckeD");
}
else
{
Response.Write("Un CheckeD");
}
}

Dynamically create textbox based on amount of strings in array

I am iterating through an array of strings.
For each string I need it to be presented in a new textbox on the web page.
I have got this working by just having one textbox available, however this worked as a proof of concept. My problem being I am dealing with larger arrays now, with a varying amount of strings.
Is there a method to dynamically create textboxes on the page in relation to how many strings there are?
Here is what I have so far-
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
WebService1 ws = new WebService1();
foreach (string x in ws.mRETURN(CheckBoxList1.SelectedItem.Text))
{
TextBox1.Text = x;
}
}
So textbox one already exists on the page, however I know need it to be dependant on how many strings there are being passed from the webservice, and relate this number to the amount of textboxes needed.
Try this
Markup
<asp:DataList runat="server" ID="repeatedTextBox">
<ItemTemplate>
<asp:TextBox ID="myTextBox" runat="server" Text='<%# Container.DataItem.ToString() %>'></asp:TextBox>
</ItemTemplate>
</asp:DataList>
Codebehind
repeatedTextBox.DataSource = myChangingArrayOfString; //e.g ws.mRETURN(CheckBoxList1.SelectedItem.Text)
repeatedTextBox.DataBind();
How about using a placeholder on the webpage and adding controls(TextBoxes) to it programatically.
<asp:PlaceHolder id="Area1" runat="server"></asp:PlaceHolder></P>
Then on your back end add:
TextBox tbx = new TextBox();
Area1.Controls.Add(tbx);
In a loop you could do that?
I am still learning so its my best suggestion at this stage.
Derived from source: Source

Passing data from gridview as session

I've a gridview with client ID, client name, client contact number and a hyperlink to client detail.
I would like to pass selected clint ID to another asp page (clientDetail.aspx & ClientContact.aspx).
I'm thinking of passing the clientID as session. But how do i go about doing this? Can someone guild me on this as im quite new to this.
& how do i use the passed data in clientDetail.aspx & ClientContact.aspx?
Thanks in advance for your help.
Add two new columns of type HyperLinkField to your gridView as follows. Now clientID is passed as QueryString. One link is here
<asp:HyperLinkField DataNavigateUrlFields="ClientID"
DataNavigateUrlFormatString="~/ClientDetails.aspx?id={0}"
Text="Client Details" />
Assuming that you are selected the grid row with a button or link button you could use the OnRowCommand event
When you wire into this event you can pick out the value you want from the selected item then save it into the Session which will then be available for subsequent pages. In the below exampel I've assumed the value is in a label field so you can pick it out of that control.
void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblMyValue = (Label)e.Row.FindControl("lblMyValue");
Session["myValue"] = lblMyValue .Text;
}
}
There are other variants of this for instance you could store the value you are interested in in the CommandArgument property of the button that you use to select the row. The command argument will then be available in the RowCommand event
void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
string arg = e.CommandArgument;
//.. put into session here
}
And there are alternatives using different events for instance you could use the DataKeys collection of the GridView to store the value you are interested in and pick out the value from there
Markup fragment
<asp:gridview id="CustomersGridView"
//.. more properties
datakeynames="myID"
onselectedindexchanged="MyGridView_SelectedIndexChanged"
runat="server">
Code behind
void MyGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
int index = MyGridView.SelectedIndex;
Session["myValue"] = CustomersGridView.DataKeys[index].Value.ToString();
}
There are a number of alternatives to get this working. I would use the first one detailed if it were me - I've always found it easiest to get to work. You can make the label hidden with Css if you want - if it isn't suitable for the UI. Or use a hidden field (with runat="server"). I'm going to stop - I'm risking confusuing by just typing on.
You should be able to evaluate the clientid field of the asp.net gridview in the navgiate url of the hyperlink and pass it as a query string like so:
<asp:gridview id="OrdersGridView"
datasourceid="OrdersSqlDataSource"
autogeneratecolumns="false"
runat="server">
<columns>
<asp:boundfield datafield="ClientID"
headertext="Client ID"/>
<asp:boundfield datafield="ClientName"
headertext="Client Name"/>
<asp:boundfield datafield="ClientContact"
headertext="Client Contact"/>
<asp:hyperlinkfield text="Details..."
navigateurl="~\details.aspx?clientid='<%# Eval("ClientID") %>'"
headertext="Order Details"
target="_blank" />
</columns>
</asp:gridview>

Select from db, making icons from the items

What I'm trying to do is basicly what this photo shows.
When I select something from the treeview it passes a parameter to a linq command that selects some data from the database. For every item in the selection I want to make a Icon and a text that represents if the item is a folder or a file.
When I push the Icon or the Link i want it to do the same as i would push the treeview, pass a parameter to a linq command that selects again from the database and populates the placeholder.
The way I'm doing this now is to make at runtima a Panel that holds the ImageButton and LinkButton. Then i add the Panel to the ContentPlaceHolder.
The problem with this that it does it every time i select something new and also i cant get it to work if the push the icon or the linkbutton, only the from the treeview.
Could i use some controller and css to get this look for the Icons ?
Is there another better way ?
This is basicly the same system as the Explorer uses in Windows, Treeview shows only the folder but the window shows the folders and files. When i click a folder that folder opens up and the main window is populated with items that are inside that folder. If i click a file a editor opens up with the contents of the file.
Not sure I understand you question 100% but I think I got the gist.
I'm assuming that you want the folders first, then the files. I would create two repeaters in this area, one to hold the Folder Image and link buttons, and the other for the file image and link buttons.
Break your linq command into two queries, one to get the folders and one for files. Then just bind the repeaters to the corresponding repeaters.
Here's a bit of code to get you started:
<asp:Repeater ID="rptFolders" runat="server" OnItemCommand="rptFolders_ItemDataBound">
<ItemTemplate>
<div>
<asp:ImageButton ID="btnImage" runat="server" />
<asp:LinkButton ID="btnLink" runat="server" />
</div>
</ItemTemplate>
</asp:Repeater>
And the code behind after calling DataBind():
protected void rptFolders_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Book book = (Book)e.Item.DataItem; //Or whatever your passing
ImageButton btnImage = e.Item.FindControl("btnImage");
LinkButton btnLink = e.Item.FindControl("btnLink");
btnLink.Text = book.Name;
btnLink.Click += new EventHandler(FolderClicked);
btnImage.Click += new ImageClickEventHandler(FolderClicked);
}
}
You can obviously do whatever you want with Click Events, just added those in for good measure.
I would probably create a Folder and File Control and use those instead of the imagebutton / linkbutton combo, this way I could store more information about the Folder / File to access them later without having to do another query to get the ID or what not. But there are a million approaches to this, pick the one you think is best.
Let me know if you need more guidance w/ this solution, or if I didn't understand your question.
Happy Coding...
Sorry had to add as another Answer. Here's a quick sample of the folder user control.
Create your Control... Format however you want.
<%# Control Language="C#" AutoEventWireup="true" CodeFile="FolderButton.ascx.cs" Inherits="FolderButton" %>
<div>
<asp:ImageButton ID="btnImage" runat="server" ImageUrl="yourfolder.jpg" />
<asp:LinkButton ID="btnTitle" runat="server" />
</div>
Add Properties and Click Event to the Code Behind (don't forget to fire the click event when your image and link buttons are clicked):
public partial class FolderButton : System.Web.UI.UserControl
{
public int DatabaseId { get; set; }
public string Name { get; set;} // you can even set your linkbutton text here.
public event EventHandler Click;
}
Create your Repeater of the FolderButton Controls:
<asp:Repeater ID="rptFolders" runat="server" OnItemDataBound="rptFolders_ItemDataBound">
<ItemTemplate>
<uc1:FolderButton ID="FolderButton1" runat="server" />
</ItemTemplate>
</asp:Repeater>
Set Folder Id on DataBinding:
protected void rptFolders_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Book book = (Book)e.Item.DataItem; //Or whatever your passing
FolderButton btnFolder = e.Item.FindControls("FolderButton1");
btnFolder.Name=book.Name;
btnFolder.DatabaseId=book.Id;
btnFolder.Click += new EventHandler(FolderClicked);
}
}
Lastly you can then do whever you want on the event Click:
void FolderClicked(object sender, EventArgs e)
{
int id = ((FolderButton)sender).DatabaseId;
/// Do something with your Id
}
Let me know if anything is unclear. This is just a quick freehand sample, so forgive any typos or bad practices... code is just for demostration purposes only.

Updating asp:SqlDataSource Parameter via asp:LinkButton

I'll try to explain what I'm doing the best I can, but I'm pretty new to asp.net so be patient with me.
I have a SqlDataSource which returns a simple select statement based on the WHERE clause using #COURSE_ID
What I want to-do is every time any one of 2 (this will change as it's going to be generated) asp:LinkButtons are pressed, they will change the #COURSEID value which i'd like to associate with the specific button.
Buttons:
<asp:LinkButton ID="LinkButton2" runat="server" onclick="MenuUpdate_Click">Course1</asp:LinkButton>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="MenuUpdate_Click">Course2</asp:LinkButton>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:connString %>" SelectCommand="SELECT Chapter.chapterName, Chapter.chapterID
FROM Chapter
WHERE Chapter.courseID = #COURSE_ID
">
C#
protected void MenuUpdate_Click(object sender, EventArgs e)
{
Parameter p = SqlDataSource1.SelectParameters["COURSE_ID"];
SqlDataSource1.SelectParameters.Remove(p);
SqlDataSource1.SelectParameters.Add("COURSE_ID", THIS NEEDS TO BE VALUE ASSOCIATED TO BUTTON);
ListView1.DataBind();
UpdatePanel1.Update();
}
If anyone has any suggestions that'd be great, I've been trying lots of different things all night with no success :(
Thanks
Try setting the linkbutton IDs to contain the course name (I dont think its possible for IDs to start with numbers, so give them IDs "crs" and then the course name). In the click event for the buttons, extract the ID of the sender and take the entire string after "crs". This is the course name of the linkbutton that was clicked.
Also if "COURSE_ID" is the only parameter of the SelectCommand, I would personally clear all the parameters first. It just ensures that you dont have any parameters from elsewhere. Thats just the way I work though - clear everything and reenter anything which shouldnt have been cleared and then enter anything extra.
Regards,
Rich

Categories

Resources