Textbox to label with two websites - c#

I have two sites in one of them i have a textbox and on the other site i have a label. What i want to do is have the value that is written in the textbox be passed on to the label in the next website on a button click.
Website1.aspx (with the textbox)
protected void btnSpara_Click(object sender, EventArgs e)
{
}
The textbox:
<asp:TextBox ID="tbxKommentar" runat="server" OnTextChanged="tbxKommentar_TextChanged"></asp:TextBox>
Website2.aspx (with the label)
<asp:Label ID="lblKommentar" runat="server"></asp:Label>
How would i go about doing this.

So what you are asking is how to pass value from one page to another. There are several options and all of them are described here https://msdn.microsoft.com/en-us/library/6c3yckfw.aspx
Code sample:
protected void btnSpara_Click(object sender, EventArgs e)
{
Response.Redirect("Website2.aspx?txt="+ tbxKommentar.Text);
}
In your Website2.aspx:
protected override void OnInit(EventArgs e){
lblKommentar.Text = Request.Params["txt"];
}

Related

How to pass an attribute value from ImageButton to code behind

On page load I add a new attribute (UploadStatus) to an ImageButton (FileUpload_result) from code behind. Now on button click I want to retrieve the value of the added attribute. How can I do that?
public string UploadStatus = "testing";
protected void Page_Load(object sender, EventArgs e)
{ FileUpload_result.Attributes.Add("UploadStatus", UploadStatus); }
<asp:ImageButton runat="server" ID="FileUpload_result" OnClick="FileUpload_Click" ImageUrl="icon-ok.png" UploadStatus="testing" />
protected void FileUpload_Click(object sender, EventArgs e)
{ // How can I get the value of the added attribute UploadStatus? The value is testing in this case}
In your FileUpload_Click method you can access the attribute like this.
((ImageButton)sender).Attributes["UploadStatus"]
Something like FileUpload_result.Attributes[""UploadStatus"] might get it. https://msdn.microsoft.com/en-us/library/kkeesb2c%28v=vs.140%29.aspx

How do capture row selected in gridview and pass username value to text box in the next page?

I would like to select the gridview and redirect to the next page where username in gridview passed to the text box in the next page when the row is being clicked. How do i do that?
My code behind (on the first page):
protected void GridView1_OnRowSelected(object sender, GridViewSelectEventArgs e)
{
var username = Convert.ToString(GridView1.DataKeys[e.NewSelectedIndex].Value);
Response.Redirect("ViewUploads.aspx?USERNAME=" +username);
}
My code on the second page:
protected void TextBoxUsername_TextChanged(object sender, EventArgs e)
{
var username = Request.QueryString["USERNAME"];
}
I assume that you're looking for the SelectedIndexChanged event of the GridView.
protected void GridView_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{
var username = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
Response.Redirect("ViewUploads.aspx?USERNAME=" +username);
}
In the next page(ViewUploads.aspx) you need to assign it to the TextBox' Text:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack && Request.QueryString["USERNAME"] != null)
this.txtUserName.Text = Request.QueryString["USERNAME"];
}
I'm not sure if you have already done that, if you want to use the DataKeys-collection in the first page, you have to set the DataKeyNames property accordingly. For example:
<asp:gridview id="GridView1"
datakeynames="UserName"
onselectedindexchanged="GridView_SelectedIndexChanging"
runat="server">
...
</asp:gridview>

Keep ASP.NET Calender Open On Month Click

I know people have asked this over the Internet before but i have not used ant Javascript and have done it very basic.
Basically when they click an (ImageButton)icon next to a Textbox it will trigger an OnClick event in the C# which will make the Calender visable. Then when they selected anything on that calender it will trigger SelectionChanged on c# agian and then will set the date they clicked on in a Textbox.
HTML
<input id="txt_DateOfInterview" type="Date" class="aclass" runat="server" />
<asp:ImageButton runat="server" imageurl="~/Images/Calender.png" id="calendericonDOI" CssClass="calendericonDOI ClanderDOI" OnClick="calendericonDOI_Click" ></asp:ImageButton>
<asp:calendar runat="server" ID="ClanderDOI" CssClass="ClanderDOI" OnSelectionChanged="ClanderDOI_SelectionChanged" BorderColor="#6a3d98">
<TitleStyle BackColor="Orange"/>
</asp:calendar>
C#
protected void calendericonDOI_Click(object sender, ImageClickEventArgs e)
{
ClanderDOI.Visible = true;
}
protected void ClanderDOI_SelectionChanged(object sender, EventArgs e)
{
txt_DateOfInterview.Value = ClanderDOI.SelectedDate.Date.ToString("d");
ClanderDOI.Visible = false;
}
Question: How can I make the calender stay visable when they click to see next month only.
Your sample code works as you expect. When you change selected month it does not trigger 'SelectionChange' event.
There is separate event for month change (VisibleMonthChanged) where you can hide calendar, but to achieve what you want just make sure that you do not hide calendar somewhere else in your code:
protected void ClanderDOI_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
{
ClanderDOI.Visible = true;
}
Using this code
Calendar will not show at Page Load
Calendar will show when you will Click SelectDate(btnSelectDate) Button
Calendar Will disappear after date selection
Calendar will not disappear on next month selection
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Calendar1.Visible = false;
}
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
Calendar1.Visible = false;
}
protected void btnSelectDate_Click(object sender, EventArgs e)
{
Calendar1.Visible = true;
}

The RowDataBound events of the gridview is not fired after turning to the next page

I use a gridview to display the search result. After clicking search button, the gridview will show page 1, but when I click page 2 link, the gridview disappeared and it was back when I click search button again and show page 2's content.
here is my code
<asp:GridView ID="searchresult" runat="server" AutoGenerateColumns="true" AllowPaging="true" OnRowDataBound="searchresult_RowDataBound" OnPageIndexChanging="searchresult_PageIndexChanging"
HeaderStyle-BackColor="#f9e4d0"
HeaderStyle-Height="20px"
Font-Size="11px"
AlternatingRowStyle-BackColor="#cfdfef"
Width="800px" style="text-align:left">
</asp:GridView>
and code behind
protected void search_Click(object sender, EventArgs e)
{
List<someclass> totalResult = new List<someclass>();
..... //some code to generate the datasource
searchresult.DataSource = totalResult;
searchresult.AllowPaging = true;
searchresult.DataBind();
}
protected void searchresult_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void searchresult_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
searchresult.PageIndex = e.NewPageIndex;
DataBind();
}
I have no idea why the page 2 won't show up until I click search button again. when I clicked the page 2 link, the page did postback but the RowDataBound event was not fired
You have to give your grid a datasource. It appears you are only doing this on search_Click, so your grid is only going to have data then. Try something like:
protected void search_Click(object sender, EventArgs e)
{
PopGrid();
}
protected void searchresult_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
searchresult.PageIndex = e.NewPageIndex;
PopGrid();
}
protected void PopGrid()
{
List<someclass> totalResult = new List<someclass>();
..... //some code to generate the datasource
searchresult.DataSource = totalResult;
searchresult.AllowPaging = true;
searchresult.DataBind();
}
searchresult_PageIndexChanging event handler will make that functionality work. However I recommend you use a gridview control inside a Panel with vertical scroll bar. My user love it and it is a lot faster moving up and down the gridview without defining any page index changing.
I hope it work for you.

Why my Query string is not working?

I am facing problem in query string. Following is my asp code
<asp:Label ID="Lable1" runat="server" Text="" ></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
C# code:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Lable1.Text += Request.QueryString["refresh"] ;
Response.Redirect("QueryString1t.aspx?refresh=" + 1 + "");
}
Up to my knowledge Lable1 text should change on button click every time. Lable1 text should not show any thing on page load . on button click it should be like for first click 1 for second click 11 and so on..
But it is not showing as my expectation .So tell me please where i am wrong?
You are redirecting after setting label text, wrong approach.
Try this: -
protected void Page_Load(object sender, EventArgs e)
{
Lable1.Text = Request.QueryString["refresh"];
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("QueryString1t.aspx?refresh=" +
string.IsNullOrEmpty(Request.QueryString["refresh"]) ? 0 :
Convert.ToInt32(Request.QueryString["refresh"]) + 1 + "");
}
OR this: -
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Lable1.Text = string.IsNullOrEmpty(Lable1.Text) ? "0" :
(Convert.ToInt32(Lable1.Text) + 1).ToString();
}
Have a look at this:
http://msdn.microsoft.com/en-us/library/ms178472.aspx
Particularly the fourth and fifth events in the cycle.
If you want the text to be updated as you are looking for there, then you need to put that code in the page load, not the click event handler.
In brief, you have to think of it like this: Every time you do a redirect, you lose viewstate,
http://msdn.microsoft.com/en-us/library/ms972976.aspx
which is the means by which ASP.NET keeps track of your updates to the controls, like in your event handler. So your update is immediately lost.
The next piece of code you will hit is your load event, so that is where you need to set the label's text property.
How do I persist the value of a label through a response.redirect?

Categories

Resources