NavigateUrl in ASP.Net - asp.net

I am new to ASP.NET. I am having problem with NavigateUrl.
<td align="right" valign="middle">
<p id="posCstmr">
<asp:HyperLink ID="hlnkContact" CssClass="addbtn-cmplist" runat="server" NavigateUrl='<%# "Actions/Contact.aspx?ContactID=" + Eval("ContactID") + "&CompanyID=" + Eval("CompanyID") %>' Text="View"></asp:HyperLink>
</p>
</td>
When I click to view following hlnkContact, it redirects to the following link with specified values which is OK.
[http://localhost:1426/Actions/Contact.aspx?ContactID=78724&CompanyID=92971]
But I want to store these values in session variables on Page_load event of Contact.aspx.
if (!Page.IsPostBack)
{
Session["ContactID"] = String.IsNullOrEmpty(Request.QueryString["ContactID"].ToString()) ? String.Empty : Session["ContactID"];
}
But I cant store the Session variable on Page_Load because NavigateUrl show the values after loading of this page.
Please Help me.

Same type of question was answered HERE, you can tweak like below:
[Assuming that the td is inside any databound container, else Eval wouldn't work]
Change your markup to this:
<td align="right" valign="middle">
<p id="posCstmr">
<asp:LinkButton ID="LinkButton1" CssClass="addbtn-cmplist" runat="server"
Text="View" CommandName="Link" CommandArgument='<%#Eval("ContactID") + ";" + Eval("CompanyID") %>'
OnClick="ButtonLink_Click" />
</p>
</td>
Code:
protected void ButtonLink_Click(object sender, System.EventArgs e)
{
LinkButton lb = (LinkButton)sender;
string[] arguments = lb.CommandArgument.Split(';');
string ContactID = arguments[0];
string CompanyID = arguments[1];
//Save in session
Session["ContactID"] = ContactID ;
Session["CompanyID"] = CompanyID ;
//Redirect
Response.Redirect(string.Format("Actions/Contact.aspx?ContactID={0}&CompanyID={1}", ContactID, CompanyID));
}

Related

Repeater causes postback of the whole page even if it's inside an update panel

I have an user control that contains an update panel with a repeater. The repeater has a button used to delete records.
The problem is when I press the delete button, and a record from the repeater gets deleted, a postback on the whole page is triggered. From my understanding, only the section inside the update panel should refresh.
I should also mention that on my master page I have a script manager with the property "EnablePartialRendering" set to true.
Can someone help me with this problem? Thanks in advance.
ASP file:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="ActivityFiles.ascx.cs" Inherits="Training.User_Controls.ActivityFiles" %>
<asp:Repeater ID="FilesRepeater" runat="server">
<HeaderTemplate>
<table class="table table-bordered">
<tr>
<td><b>Name</b></td>
<td><b>Description</b></td>
<td><b>Actions</b></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<tr>
<td><%# Eval("Name")%></td>
<td><%# Eval("Description")%></td>
<td>
<asp:LinkButton ID="DownloadFile" runat="server" OnClick="DownloadFile_Click" Font-Size="Large"
file-path='<%# Eval("Url")%>'>
<span class="glyphicon glyphicon-floppy-disk text-info" aria-hidden="true"></span>
</asp:LinkButton>
<asp:LinkButton ID="DeleteFile" runat="server" OnClick="DeleteFile_Click" Font-Size="Large"
file-id='<%# Eval("Id") %>'
file-path='<%# Eval("Url") %>'>
<span class="glyphicon glyphicon-trash text-danger" aria-hidden="true"></span>
</asp:LinkButton>
</td>
</tr>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
.cs File:
protected void Page_Load(object sender, EventArgs e)
{
activityId = (int)Context.Items["activityId"];
if ( ! IsPostBack && Visible)
{
if (activityId != 0)
{
LoadFiles();
}
}
}
private void LoadFiles()
{
DataTable files = DatabaseHelper.SelectAsDatatable(FilesQueries.GET_ACTIVITY_FILES, new Dictionary<string, object> {
{ "activityId", activityId }
});
FilesRepeater.DataSource = files;
FilesRepeater.DataBind();
if (files.Rows.Count == 0)
{
FilesRepeater.Visible = false;
NoRecords.Visible = true;
}
}
protected void DeleteFile_Click(object sender, EventArgs e)
{
// TODO: modify this function to work only with the fild id
LinkButton deleteButton = (LinkButton)sender;
string fileId = deleteButton.Attributes["file-id"];
string filePath = deleteButton.Attributes["file-path"];
DatabaseHelper.ExecuteNonQuery(FilesQueries.DELETE_FILE, new Dictionary<string, Object>() {
{ "#fileId",fileId },
});
//delete file from disk
string path = Server.MapPath(filePath);
FileInfo file = new FileInfo(path);
if (file.Exists)
{
file.Delete();
}
LoadFiles();
}
Found the solution on another stackoverflow post (See Eugene S answer)
You have to manually add you control to Script Manager. You should do that inside File Repeater's Item created event. Good luck.

dropdownlist always returns first value even with EnableViewState set to true

My dropdown list control always returns the first item on postback, I've tried every solution i've come across but to no avail.
Bascially, I have two data classes that are simply containers for data.
ConnectedRobots (representing connected robot with its controller's version, its IP address and some other properties), and MiseAJour (representing a single available update with its version and some other details describing it).
These two classes are then used to create objects representing each connected robot or update available. I'm then creating a List of update versions to use it as a data source for each Dropdown list.
as shown in the picture below :
Click to view
Here's the piece of code reflecting this view (Default.aspx) :
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplicationTest.WebForm1" Theme="Theme1" EnableEventValidation="false" EnableViewState="true"%>
...
<body>
<form runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
...
<div style="width:100%;">
<table class="table table-hover">
<thead>
<tr>
<th class="auto-style1">Référence Robot/Version Contrôleur</th>
<th class="auto-style1">Pays/Adresse IP</th>
<th class="auto-style1">Etat</th>
<th class="auto-style1">Versions des contrôleurs disponibles</th>
<th class="auto-style1">Planification</th>
<th>MAJ</th>
</tr>
</thead>
<tbody>
<asp:Repeater ID="repCRobots" runat="server" EnableViewState="true">
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblReferenceRobot" runat="server" Text='<%# Eval("ReferenceRobot") + " / " %> ' />
<asp:Label ID="lblVersionControleur" runat="server" Font-Bold="true" Text='<%# Eval("VersionControleur") %>'/>
</td>
<td>
<asp:Label ID="lblPays" runat="server" Text='<%# Eval("Pays") + " / " %> ' />
<asp:Label ID="lblIPRobot" runat="server" Font-Bold="true" Text='<%# Eval("IPRobot") %>'/>
</td>
<td>
<asp:Label ID="lblEtat" runat="server" Font-Bold="true" Text='<%# Eval("Etat") %>'/>
</td>
<td>
<asp:DropDownList ID="VersionsMAJs" runat="server" CssClass="bg-primary" EnableViewState="true" OnSelectedIndexChanged="VersionsMAJs_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
</td>
<td>
<span style="padding: 0px 10px 10px 10px">
<cc1:TimeSelector ID="TimeSelector1" runat="server" DisplayButtons="false" Font-Bold="true" BackColor="#cce6ff" BorderStyle="Dotted" CssClass="bg-info" BorderColor="White"></cc1:TimeSelector>
</span>
</td>
<td>
<asp:CheckBox ID='SelectMAJ' runat="server"/>
<asp:HiddenField ID="HiddenCheckBox" Value='<% #Eval("ReferenceRobot")%>' runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
<asp:Button ID="MAJButton" runat="server" Text="Mettre à jour" CssClass="btn btn-success btn-lg btn-block"/>
</div>
And the code behind is the following :
public partial class WebForm1 : System.Web.UI.Page
{
protected List<MiseAJour> ListeMAJs;
protected List<string> ListeVersionsMAJs;
protected List<ConnectedRobots> ListeRobotsConnectes;
protected List<string> ListeRefRobots;
protected DropDownList ddlVersionMAJ;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetConnectedRobots();
Repeater repCRobots = this.FindControl("repCRobots") as Repeater;
repCRobots.DataSource = ListeRobotsConnectes;
repCRobots.DataBind();
foreach (RepeaterItem item in repCRobots.Items)
{
ddlVersionMAJ = item.FindControl("VersionsMAJs") as DropDownList;
ddlVersionMAJ.DataSource = ListeVersionsMAJs;
ddlVersionMAJ.DataBind();
}
}
}
private void GetConnectedRobots()
{
WebserviceRobots ws = new WebserviceRobots();
string[] delimiters = new string[] { "|", "||" };
string connectedRobots = ws.getConnectedRobots();
string majsDetails = ws.getMajsDetails();
string[] connectedRobotsInfos = connectedRobots.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
string[] majsInfos = majsDetails.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
ListeRobotsConnectes = new List<ConnectedRobots>();
ListeRefRobots = new List<string>();
ListeMAJs = new List<MiseAJour>();
ListeVersionsMAJs = new List<string>();
for (int i = 0; i < majsInfos.Length - 1; i += 3)
{
ListeMAJs.Add(new MiseAJour() { VersionMAJ = majsInfos[i], DetailsMAJ = majsInfos[i + 1], Commentaires = majsInfos[i + 2] });
}
ListeVersionsMAJs = ListeMAJs.Select(v => v.VersionMAJ).ToList();
for (int i = 0; i < connectedRobotsInfos.Length - 1; i += 5)
{
ListeRobotsConnectes.Add(new ConnectedRobots() { ReferenceRobot = connectedRobotsInfos[i], VersionControleur = connectedRobotsInfos[i + 1], IPRobot = connectedRobotsInfos[i + 2], Pays = connectedRobotsInfos[i + 3], Etat = connectedRobotsInfos[i + 4], VersionsMAJ = ListeVersionsMAJs });
}
ListeRefRobots = ListeRobotsConnectes.Select(r => r.ReferenceRobot).ToList();
}
protected void VersionsMAJs_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList dll = (DropDownList)sender;
string test = dll.SelectedValue;
}
the GetConnectedRobots() method is used to retrieve both update versions, and connected robots using a web service and couple of dynamically generated xml files.
On selectedIndexChanged, I should get the selected value or so I thought. SelectedValue always returns the first element of the dropdown list. What am I doing wrong?
I do have EnableViewState="true" for persistence between postbacks.
I am open to all kinds of answers, even if it means restructuring the whole code,
Thank you for your answers,
So I resolved the issue after almost going insane, the problem weirdly enough was with getMajDetails() implemented in the WebserviceRobots. This method is used to deserialise an XML document containing updates versions and some other informations, and to return a string with these elements seprated by | character. However, it seems like XDocument automatically adds new line char at the end of each element, resulting in a string similar to this one :
1.7.1940\r\n| Détails concernant le MAJ 1.7\r\n| Commentaires 1.7\r\n||2.0.2542\r\n| Détails concernant le MAJ 2.0\r\n| Commentaires 2.0\r\n||1.8.2495\r\n| Détails concernant le MAJ 1.8\r\n| Commentaires 1.8\r\n||
So, each ListeVersionsMAJs element ends up with \r\n at the end. and When this list is used to populate the dorpdown list, it causses it to behave the way it did (returning the first value at each postback). the solution was to remove \r\n .
I have no explanation as to why this is happening, testing the dropdown list with a List of strings initialized with fixed string values with new line char at the end of each value resulted in the same odd behavior.
So if anyone could clear this up, it would be much appreciate it.

Unable to pass data from one content page to another using master page

I'm trying to make an asp webform that posts data to another webform.
I've made two separate projects, one that uses master page and one the doesn't.
Senario:
WebForm1.aspx has two text boxes and a submit button
<table>
<tr>
<td >Name:</td>
<td >
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td class="auto-style1"></td>
</tr>
<tr>
<td>Id:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</td>
<td> </td>
</tr>
</table>
WebForm2.aspx.cs has two labels which should display the data received from WebForm1.aspx
Page prevPage = this.PreviousPage;
if (prevPage != null)
{
Label1.Text = ((TextBox)prevPage.FindControl("TextBox1")).Text;
Label2.Text = ((TextBox)prevPage.FindControl("TextBox2")).Text;
}
Case 1: [Posting without master page]
The data is posted normally.
Case 2: [Posting with master page]
I get NullReferenceException.
So I broke down the code.
Page prevPage = this.PreviousPage;
if (prevPage != null)
{
ControlCollection collec = prevPage.Controls;
Control ctrl= prevPage.FindControl("TextBox1");
TextBox txtbx = (TextBox)ctrl;
Label1.Text = txtbx.Text; //Exception raised here
Label2.Text = ((TextBox)prevPage.FindControl("TextBox2")).Text;
}
While debugging:
I executed "collec.Count" in the Immediate Window.
Case 1: [Posting without master page]
collec.Count returned 5
Case 2: [Posting with master page]
collec.Count returned 1 [ WHY? ]
Later,
I tried to pass data using public properties
WebForm1.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("WebForm2.aspx");
}
public string Name { get { return TextBox1.Text; } }
public string ID { get { return TextBox2.Text; } }
WebForm2.aspx.cs
WebForm1 prevPage = (WebForm1)this.PreviousPage;
if (prevPage != null)
{
ControlCollection c = prevPage.Controls;
Label1.Text = prevPage.Name;
Label2.Text = prevPage.ID;
}
and now it works correctly, even with master page.
So could anybody explain me whats going on and why posting from one content page to another content page with master giving me NullReferenceException ?
First you must look in the Content Placeholder of the submitting page
Therefore, the code would look more like this:
ContentPlaceHolder placeHolder = (ContentPlaceHolder)PreviousPage.Master.FindControl("ContentPlaceHolder1");
TextBox txt1 = (TextBox)placeHolder.FindControl("TextBox1");
When you are using master page a TextBox with an id of TextBox1 inside a Content control tied to ContentPlaceHolder1 will have its id attribute extended like this:
<input name="ctl00$ContentPlaceHolder1$TextBox1" type="text" id="ContentPlaceHolder1_TextBox1" />
But when you are not using Master page there is no 'ContentPlaceHolder' so TextBox1 will rendered like this:
<input name="TextBox1" type="text" id="TextBox1" />

clearing checkboxlist

on my save button i want to clear all values of form i did the following for CheckBoxList beach.
But it doesn't work. Why so, it doesn't make values clear for checkbox list
Branch is filled like this:
protected void course_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
int courseId = Convert.ToInt32(course.SelectedValue);
DataTable dt;
dt = placementManager.GetBranchList(courseId);
if (dt.Rows.Count != 0)
{
Branch.DataSource = dt;
Branch.DataTextField = "branch_name";
Branch.DataValueField = "branch_id";
Branch.DataBind();
}
Btnsave.Visible = false;
GridView1.Visible = false;
}
catch (Exception ex)
{
COMMON.logger.Error("Error on course_SelectedIndexChanged:CompanySelected.aspx.cs", ex);
}
if (b)
{
gridNotExist.Text = "Records Successfully inserted for the displayed table, To insert new records select new entries";
this.ViewState.Remove("selectedList");
this.ViewState.Remove("dt");
Session.Abandon();
passout.SelectedIndex = 0;
company.SelectedIndex = 0;
txtpackage.Text = "";
course.SelectedIndex = 0;
Branch.DataSource = null;
Branch.DataBind();
vistDate.Text = "";
txtvenue.Text = "";
//GridView1.Visible = true;
}
}
aspx page has, of course, branch like this:
<asp:UpdatePanel id="update" runat="server">
<contenttemplate>
<td>
<asp:DropDownList ID="course" runat="server" AutoPostBack="True" OnSelectedIndexChanged="course_SelectedIndexChanged" />
<asp:CustomValidator ID="coursenecessaryForSaveButton" runat="server" ControlToValidate="course" ErrorMessage="Select A Course" OnServerValidate="coursenecessaryForSaveButton_ServerValidate" ValidationGroup="save" />
</td>
<td>
<asp:CustomValidator ID="courseNecessary" runat="server" ErrorMessage="Select A Course" OnServerValidate="courseNecessary_ServerValidate" ValidationGroup="verify" />
</td>
<td style="width: 101px">
Branch Name*
</td>
<td style="width: 126px">
<asp:CheckBoxList id="Branch" runat="server" />
</td>
</contenttemplate>
</asp:UpdatePanel>
<asp:CheckBoxList id="Branch" runat="server" >
is in a different UpdatePanel to the control which is triggering the asynchronous postback - and firing your eventhandler: course_SelectedIndexChanged.
Put the Checkbox List inside the UpdatePanel with id="update" and it will solve your problem.
Still, i would consider refactoring your HTML (and code while you're at it).
For instance - you have two UpdatePanel's - why?
The second one has nothing in it besides checkboxes. The purpose of an UpdatePanel is to dynamically update portions of a page (ie dynamic information) without a full postback.
Having nothing but static controls (checkboxes) inside an UpdatePanel isn't really serving any purpose.
However, my above answer is a stab in the dark at what you're after. You're code-behind code does'nt really make sense:
if (b == true)
Where is b defined??
Branch.DataSource = null
Branch.DataBind()
What are you binding??

ListView fields not getting posted

I know I've done something like this before, but I have no idea why it isn't working now. I have a ListView with some textboxes. I want to read the text out of those boxes when I click a button (linkbutton, whatever).
<asp:ListView runat="server" ID="lv_bar" EnableViewState="true">
<LayoutTemplate>
<table>
<tr>
<th>Foo</th>
</tr>
<tr runat="server" id="itemPlaceholder"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><asp:LinkButton ID="lb_delete" CausesValidation="false" runat="server" Text="Del" /></td>
<td><asp:TextBox id="txt_foo" runat="server" /></td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:LinkButton ID="lb_add" CausesValidation="false" runat="server" Text="Add" />
And then here's the relevant code-behind stuff:
protected void Page_Load(object sender, EventArgs e)
{
lb_chapter_add.Click += lb_chapter_add_Click;
if (!IsPostBack)
{
lv_chapters.DataSource = new List<Foo>() { new Foo() { Name = "harbl"} };
lv_chapters.DataBind();
}
}
void lb_add_Click(object sender, EventArgs e)
{
foreach (ListViewDataItem item in lv_bar.Items)
{
var txt_foo = (TextBox)item.FindControl("txt_foo");
Response.Write("foo: " + txt_foo.Text);
}
Response.Write("<br />the end");
Response.End();
}
But what I see when I enter some text into txt_foo and click lb_add is just "the end". What am I doing wrong here?
The problem it that you are using a a non persistent object as DataSource.
Due to clicking the button, you generate a postback and lv_chapters does not contain any items. Set a breakpoint in the line where the foreach is and you will see that lv_chapters.Items in null, or that it's Count property returns 0.

Resources