I am retrieving data from table column where I have saved tags for the article, for example: "animals dogs cats" and showing this string text in linkbutton. If I click it, then page is redirected to "Tags.aspx?name=animals dogs cats".
Is it possible to redirect page to "Tags.aspx?name=cats"
if I have clicked on "cats", or maybe to split the string and show each word in own linkbutton (without using something like listview)?
Thanks, Oak
If you don't want to use a web-databound control like Repeater you can create the LinkButtons dynamically. Remember to recreate them on postbacks with the same ID as before in Page_Load at the latest:
protected void Page_Init(object sender, EventArgs e)
{
createTagButtons();
}
private void createTagButtons()
{
var tblTags = new DataTable();
using (var con = new SqlConnection(connectionString))
using (var da = new SqlDataAdapter("SELECT TagID, TagName FROM dbo.Tags ORDER BY TagName", con))
{
da.Fill(tblTags);
}
foreach (DataRow row in tblTags.Rows)
{
int tagID = row.Field<int>("TagID");
string tagName = row.Field<string>("TagName");
LinkButton tagButton = new LinkButton();
tagButton.ID = "tagButton_" + tagID;
tagButton.CommandArgument = tagName;
tagButton.Click += TagLinkClicked;
this.TagPanel.Controls.Add(tagButton);
}
}
private void TagLinkClicked(Object sender, EventArgs e)
{
LinkButton tagLink = (LinkButton)sender;
string url = string.Format("Tags.aspx?name={0}", tagLink.CommandArgument);
Response.Redirect(url);
}
On the aspx you could use a Panel:
<asp:Panel ID="TagPanel" runat="server"></asp:Panel>
You can use string[] words = your_string.Split(); and then create buttons using loop
A link will show only one view called from the controller.
That is, if you have a tag "cat" and this tag is related with some view then this tag calls that view which is written in the controller.
Related
In my web application, i am trying to do show the selected items as comma separated in the Multi select drop down text field is it possible to do and how can i do this can any one tell me. sample image is link [As Shown in figure]
I tried:
.aspx:
<asp:DropDownCheckBoxes ID="dropdown1" runat="server" UseSelectAllNode="true" UseButtons="true" OnSelectedIndexChanged="dropdown1_SelectedIndexChanged" AutoPostBack="true"><Style DropDownBoxBoxWidth="200"/></asp:DropDownCheckBoxes>
.CS
public void ddlsbind()
{
string str=string.Empty;
string query = "select distinct col1 from Collection where sample='" + ddl2.SelectedItem.Text.ToString() + "' and col1 IS NOT NULL";
SqlCommand cmd = new SqlCommand(query, con);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
ddlInsurance.DataSource = dt;
ddlInsurance.DataTextField = "col1";
ddlInsurance.DataValueField = "col1";
ddlInsurance.DataBind();
}
Can anyone help me to how to do this.
Thank you
psuedoCode:
private void dropdown1_SelectedIndexChanged(object sender, EventArgs e){
List<string> checkedList = new List<string>();
foreach(checkbox c in dropdown1){
if(c.Checked){
checkedList.Add(c.Value/Text/Whatever);
}
}
myTextField.Text = String.Join(",",checkedList);
}
The code you're looking for is specifically set forth in the documentation for your control:
protected void btnSubmit_Click(object sender, EventArgs e)
{
List<String> CountryID_list = new List<string>();
List<String> CountryName_list = new List<string>();
foreach (System.Web.UI.WebControls.ListItem item in ddchkCountry.Items)
{
if (item.Selected)
{
CountryID_list.Add(item.Value);
CountryName_list.Add(item.Text);
}
lblCountryID.Text = "Country ID: "+ String.Join(",", CountryID_list.ToArray());
lblCountryName.Text = "Country Name: "+ String.Join(",", CountryName_list.ToArray());
}
}
See more at: http://www.dotnetfox.com/articles/dropdowncheckboxes-or-drop-down-checkboxlist-control-in-Asp-Net-1100.aspx#sthash.PUd43B3a.dpuf
In your specific case, since text and value are the same, you only need one list:
protected void dropdown1_SelectedIndexChanged(object sender, EventArgs e)
{
List<String> checkedList = new List<string>();
//List<String> CountryName_list = new List<string>();
foreach (System.Web.UI.WebControls.ListItem item in dropdown1.Items)
{
if (item.Selected)
{
//CountryID_list.Add(item.Value);
checkedList.Add(item.Text);
}
//I don't know the ID/name of your textbox. You need to change that here:
myTextBox.Text = String.Join(",", checkedList);
//lblCountryName.Text = "Country Name: "+ String.Join(",", CountryName_list.ToArray());
}
}
Change the textbox from MyTextBox to the actual name of the textbox you are using and that should do it. Take a look at how I modified the sample code and use that to modify your own from here on out. Look at what it's doing.
Additional Information remember that the control you are using is custom, so it will have properties/methods/settings not fully available through VS intellisense in the manner that standard controls are. You'll have to visit http://saplin.blogspot.com/2011/05/dropdowncheckboxes-using-control.html to see how to manage your custom control.
Finally, I'd suggest staying away from custom controls until you understand a little bit more about coding .Net Web Applications - stick to standard controls with well-documented features/parameters that you can easily look up from multiple sources. Venture into custom controls (or create your own - they're easy) after you've mastered the basics.
I am making a Crystal-Report from data of GridView. The GridView has three columns in which two columns are filled with values coming from DataBase in a label and third column has a CheckBox which on check sends the value of these two columns as query string to the .cs page of Crystal-Report. The column value is used by the stored-procedure and then result is shown in the Crystal-Report.
Now my problem is if user checks two CheckBox then the value of both columns should be sent to Crystal-Report page as array. How to implement this
My code behind in GridView page is
protected void ChkCity_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in gdCityDetail.Rows)
{
bool ischecked = ((System.Web.UI.WebControls.CheckBox)row.Cells[0].FindControl("CheckBox2")).Checked;
if (ischecked == true)
{
int getrow = Convert.ToInt32(e.CommandArgument);
Label label1 = (Label )gdVilDetail.Rows[getrow].FindControl("label1");
Label label2= (Label )gdVilDetail.Rows[getrow].FindControl("label2");
Response.Redirect("~/crystal report/Landowner.aspx?Yojna=" + label1.text + "&Village=" + label2.text);
}
and the code on .cs page of Crystal-Report is
private void cityreport()
{
SqlCommand Cmd = new SqlCommand("Showvillage", Constr1);
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Parameters.Add("#Yojna_No", Request.QueryString[0]);
Cmd.Parameters.Add("#Village_Code", Request.QueryString[1]);
DataSet ds = new DataSet();
I think you cannot pass Array or any other Objects through Query string except string values. Rather you can use Session Variable.
Try this..
In page One.
//Set
Session["Variable"] = ArrayObj;
In page Two.
//If String[]
string[] arry = (string[])Session["Variable"];
hope it helps..
Check this link. There is a code in the link which has used array to send the value with querystring. I hvn't tried that but may be that will work too.
http://www.codeproject.com/Questions/298718/array-in-query-string
I am currently working on my first website. The problem I have encountered is the following: I have created a paged titled "CustomerList" that obviously list customers from an sql server DB. I have made the records clickable without using the select button that is included in the GridView. When I click a record, it takes me to my desired page. However, I can't seem to find a way to write an sql query from that click event to my desired page. The following is my code for the click event:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
if (row.RowIndex == 0)
{
Response.Redirect("UsageHistoryPage.aspx? EntityID="
+ row.Cells[0].Text);
}
}
As you can see, when I click an index, I go to the "UsageHistoryPage". I've also added row.cells.[0] to take that data and write it to the UsageHistoryPage, which in my case, would be the customers name at the top of the page. Can someone please help me with this? Does anyone have a good tutorial link? thanks.
You can try with this code - Add this code on your UsageHistoryPage
var input = Response.QueryString["EntityId"];
var connectionString = "...";
var queryString = "SELECT Name FROM TABLE WHERE COLUMN=#EntityId";
using (SqlConnection connection = new SqlConnection(
connectionString))
{
connection.Open();
using(var command = new SqlCommand(queryString, connection))
{
Command.Parameters.AddWithValue("#EntityId",input)
SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
Response.Write(String.Format("For Rick Your Name is here{0}", reader[0]));
}
}
}
#Rick you may want to look at how to check if a row is selected for example you don't need the for loop but in my case example need to check if RowIndex is = 1 or > 0 not ==0
private void myButton_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in usersGrid.Rows)
{
if (this.usersGrid.SelectedRows.Count == 1)
{
// get information of 1st column from the row
string selectedUser = this.usersGrid.SelectedRows[0].Cells[0].ToString();
}
}
}
What you need to do is something like this to make sure that a Row has been selected
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
DataGridViewRow row = GridView1.SelectedRow;
if (this.row.SelectedRows.Count == 1)
{
Response.Redirect("UsageHistoryPage.aspx?EntityID=" + row.SelectedRows[0].Cells[0].ToString());
}
}
If you want to have the Customer's name show up, on the Page Load of the UsageHistorPage.aspx add this code
String strCustName = Request.QueryString["EntityID1"];
this.Page.Title = strCustName;
try something like that.. please let someone here know if any of the ideas are working for you.
I am creating a website. The home page has a text box and drop down box in which the user enters the movie name and language to search. When the user clicks on the Search button the search result page is displayed and the results of search should be displayed in a data grid. I created session variables to pass the text of the text box and data grid to be used in the other page. The code to fetch data from the database is in a class how do i pass the values received from the database to a method of another page? This is the code i have written, it doesn't give any errors but the data grid does not get filled with results what am I doing wrong?
//Code for search button in home page
protected void Btnsearch_Click(object sender, EventArgs e)
{
Response.Redirect("SearchResults.aspx");
Session["moviename"] = TextBox3.Text;
Session["language"] = DropDownList1.Text;
}
//Code to fetch data from database
public class movie
{
public SqlDataAdapter searchmovie(object moviename, object language)
{
connection.con.Open();
SqlDataAdapter adapter1 = new SqlDataAdapter("select
select movieName,language,director,price from movie
where moviename = '" + moviename + "' and
language = '" + language + "'",
return adapter1;
}
}
//Code in search page to fill data grid with search results
public partial class SearchResults : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
movie m = new movie();
SqlDataAdapter movieDetails = m.searchmovie(Session["moviename"],
Session["language"]);
DataSet data = new DataSet();
movieDetails.Fill(data, "movieD");
GridView1.DataSource = data.Tables["movieD"];
GridView1.DataBind();
}
}
Set the session variables before redirecting, like this:
protected void Btnsearch_Click(object sender, EventArgs e)
{
Session["moviename"] = TextBox3.Text;
Session["language"] = DropDownList1.Text;
Response.Redirect("SearchResults.aspx");
}
I would like to suggest to avoid Session as data storage.
ASP.NET has a nice feature called cross-posting: it make you able all the page control and state from a page to another.
http://msdn.microsoft.com/en-us/library/ms178139.aspx
Personally, I really love feature because you can refer to page as object, having controls exposed ad property!
In my sharepoint web-part application. I am dynamically generating LinkButtons as below. and this works fine
foreach (var pName in productTypesNames[productType] )
{
var subLi = new HtmlGenericControl("li");
var linkButton = new LinkButton{ Text = pName };
linkButton.Click += new EventHandler(linkButton_Click);
subLi.Controls.Add(linkButton);
ul.Controls.Add(subLi);
}
However, when I click on one of the links in UI, my debugger never hits the breakpoint that is set at very first line of
void linkButton_Click(object sender, EventArgs e)
{
}
More Code
protected void StateClicked(object sender, CommandEventArgs e)
{
//Generate a dictionary of type Dictionary<string, List<string>>
//Display the dictionary
foreach (var productType in productTypesNames.Keys)
{
var li = new HtmlGenericControl("li");
nav.Controls.Add(li);
var ul = new HtmlGenericControl("ul");
var anchor = new HtmlGenericControl("a");
anchor.Attributes.Add("href", "#");
foreach (var pName in productTypesNames[productType] )
{
var subLi = new HtmlGenericControl("li");
var linkButton = new LinkButton{ Text = pName };
linkButton.Click += new EventHandler(linkButton_Click);
subLi.Controls.Add(linkButton);
ul.Controls.Add(subLi);
}
anchor.InnerHtml = productType;
li.Controls.Add(anchor);
li.Controls.Add(ul);
}
}
Where stateClicked is called by a click on the image map of USA.
You probably aren't recreating the dynamically generated links on every postback.
If you have a if (!IsPostback) wrapped around your foreach, try removing it.
i had the same problem here ....
i was creating an HtmlTable after firing an event...
this table has (n) HtmlTableRows (calculated in the event handler)
now each row contains 2 LinkButtons that are generated from the code behind .. after an event is handled...
and each LinkButton is assigned a new event handler:
lnkbtnEdit.CommandArgument = b.BookID.ToString();
lnkbtnEdit.Click += new EventHandler(lnkbtnEdit_Click);
where lnkbtnEdit_Click signature is as follows:
protected void lnkbtnEdit_Click(object sender, EventArgs e)
the weird thing is that .. there is a postback when i click the generated LinkButton... but the event was not firing...
i don't know exactly what the problem was ... but i found the solution: ..
it appears as if these generated controls disappear on the postback (tried to assign an id and used Page.FindControl() witch returned with null !!)
so i had to re-link the buttons.... on the Page_Load i re-generated the LinkButtons, with same ID... and linked them to their respective EventHandlers