Textbox will not update after editing - c#

I have a page called info.aspx where a user inputs his/her information. After the information entered it goes to the next page called confirm.aspx where a user can confirm details or click on an edit button an it will take them back to the info.aspx where they can edit the information.
Once I click the Edit button it will prefill the values from the session variables. But the problem is that if I change the Last Name in the textbox and it goes to the next page it will not update, it keeps the old value. I debug and saw that tbLastName for example keeps the same Last Name even tho I change it. Am I missing something here?
I have this stored in Session Variables.
info.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Session["FirstName"] != null)
{
tbFirstName.Text = Session["FirstName"].ToString();
tbLastName.Text = Session["LastName"].ToString();
tbDOB.Text = Session["DOB"].ToString();
tbEmail.Text = Session["Email"].ToString();
tbTelephone.Text = Session["Telephone"].ToString();
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
Session["FirstName"] = tbFirstName.Text;
Session["LastName"] = tbLastName.Text;
Session["DOB"] = tbDOB.Text;
Session["Email"] = tbEmail.Text;
Session["Telephone"] = tbTelephone.Text;
Response.Redirect("~/appointments/confirm.aspx");
}
info.aspx
<table class="warning" style="width: inherit;">
<tr>
<td>
First Name:
</td>
<td>
<asp:TextBox ID="tbFirstName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator2" ControlToValidate="tbFirstName"
Text="*" ForeColor="Yellow" ValidationGroup="InsertInformation" />
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<asp:TextBox ID="tbLastName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator3" ControlToValidate="tbLastName"
Text="*" ForeColor="Yellow" ValidationGroup="InsertInformation" />
</td>
</tr>
<tr>
<td>
Date of Birth:
</td>
<td>
<asp:TextBox ID="tbDOB" runat="server"></asp:TextBox>
<ajaxToolkit:MaskedEditExtender ID="MaskedEditExtender1" runat="server" Mask="99/99/9999"
AcceptNegative="None" MessageValidatorTip="true" TargetControlID="tbDOB" ClearMaskOnLostFocus="false"
ClearTextOnInvalid="false" MaskType="Date" />
<ajaxToolkit:MaskedEditValidator ID="MaskedEditValidator5" runat="server" ControlExtender="MaskedEditExtender1"
ControlToValidate="tbDOB" Display="Dynamic" ForeColor="Yellow" EmptyValueBlurredText="*"
InvalidValueBlurredMessage="*" ValidationGroup="InsertInformation" />
</td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<asp:TextBox ID="tbEmail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" ControlToValidate="tbEmail"
Text="*" ForeColor="Yellow" ValidationGroup="InsertInformation" />
</td>
</tr>
<tr>
<td>
Telephone:
</td>
<td>
<asp:TextBox ID="tbTelephone" runat="server"></asp:TextBox>
<ajaxToolkit:MaskedEditExtender ID="MaskedEditExtender" runat="server" Mask="(999)999-9999"
AcceptNegative="None" MessageValidatorTip="true" TargetControlID="tbTelephone"
MaskType="Number" ClearMaskOnLostFocus="false" ClearTextOnInvalid="false" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnNext" runat="server" Text="Next" OnClick="btnNext_Click" ValidationGroup="InsertInformation" />
</td>
</tr>
confirm.aspx.cs
protected void btnEditInfo_Click(object sender, EventArgs e)
{
Response.Redirect("/info.aspx");
}

It's rebinding the text boxes when you post back.
Try this:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["FirstName"] != null && !IsPostback)
{
tbFirstName.Text = Session["FirstName"].ToString();
tbLastName.Text = Session["LastName"].ToString();
tbDOB.Text = Session["DOB"].ToString();
tbEmail.Text = Session["Email"].ToString();
tbTelephone.Text = Session["Telephone"].ToString();
}
}

When clicking on BtnNext, Page_Load event of info.aspx.cs is invoked before the Redirect. You just need to check !IsPostBack property in Page_Load of info.aspx.cs, just like jasonwarford mentioned. That way the textbox is not reset:
if (!IsPostBack)
{
tbFirstName.Text = Session["FirstName"].ToString();
tbLastName.Text = Session["LastName"].ToString();
tbDOB.Text = Session["DOB"].ToString();
tbEmail.Text = Session["Email"].ToString();
tbTelephone.Text = Session["Telephone"].ToString();
}

Related

How to display selected row value/text inside the dropdown

So I am new to using ASP.net and i have a hard time displaying the value/text of the selected row inside the dropdown. I just want to select a row in the griview and when i select, the values in it should populate the textboxes and dropdown, can someone help me ? thanks
this is my code behind, as you can see in the LoadData() part I used the .findtext() but it outputs an error :Object reference not set to an instance of an object.
protected void grdRecentCases_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';this.style.cursor='Pointer'";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grdRecentCases, "Select$" + e.Row.RowIndex);
}
}
protected void grdRecentCases_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
LoadData(Convert.ToInt32(e.CommandArgument));
}
}
///<summary> LoadData is used to populate inputboxes with the value of the selected griview row</summary>
///<param name="rowNumber"> Indicates whether there is a selected row or non</param>
private void LoadData(int? rowNumber = null)
{
//if rowNumber is null use GridView1.SelectedIndex
var index = rowNumber ?? grdRecentCases.SelectedIndex;
//Populate the input box with the value of selected row.
GridViewRow gr = grdRecentCases.Rows[index];
txtDepartmentCase.Text = gr.Cells[2].Text;
txtLabCase.Text = gr.Cells[5].Text;
txtIncidentReportDate.Text = gr.Cells[6].Text;
txtCaseKey.Text = gr.Cells[1].Text;
drpDepartment.Items.FindByText(gr.Cells[3].Text).Selected = true;
drpCharge.Items.FindByText(gr.Cells[4].Text).Selected = true;
}
Html code
<table class="style2">
<tr>
<td class="style3">
Department Case #
</td>
<td>
<asp:TextBox ID="txtDepartmentCase" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Department
</td>
<td>
<asp:DropDownList ID="drpDepartment" runat="server" Height="18px" Width="153px" Enabled="False"
AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="drpDepartment_SelectedIndexChanged"
Visible="true">
</asp:DropDownList>
<asp:TextBox ID="txtDepartment" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Charge
</td>
<td>
<asp:DropDownList ID="drpCharge" runat="server" Height="22px" Width="153px" Enabled="False"
AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="drpCharge_SelectedIndexChanged"
Visible="true">
</asp:DropDownList>
<asp:TextBox ID="txtCharge" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Lab Case #
</td>
<td>
<asp:TextBox ID="txtLabCase" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Incident Report Date
</td>
<td>
<asp:TextBox ID="txtIncidentReportDate" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
</table>
As you mentioned, to select a row in the griview and when i select, the values in it should populate the textboxes and dropdown.
Hope this can help you.
Create a datagridview event, RowHeaderMouseClick
private void datagridview_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
string selectedDoc = datagridview.Rows[datagridview.SelectedRows[0].Index].Cells["column name"].Value.ToString() ;
combobox.Text = selectedDoc;
}

How to capture button click event from asp.net DataList

I have a DataList control in asp.net webform, inside this DataLIst I have 2 labels that are bound to database, and one button.
One of the label represent Id and other one Stock.
I want to capture the button click event of specific product, and then add that product to the user cart.
Here is my datalist:
<asp:DataList ID="dListProduct" runat="server" RepeatColumns="4" OnItemCommand="dListProduct_ItemCommand">
<ItemTemplate>
<div>
<table class="table-responsive" border="1">
<tr>
<td>
<asp:Label runat="server" Text="Available Stock: " ></asp:Label><asp:Label ID="Label1" runat="server" Text='<%# Eval("Stock")%>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" Text="Id" ></asp:Label><asp:Label ID="lblPId" runat="server" Text='<%# Eval("Product_Id")%>' Visible="false"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Button id="btnAddtoCart" runat="server" Text="Add to Cart"/>
</td>
</tr>
</table>
</div>
</ItemTemplate>
</asp:DataList>
Here is the code I am using in OnItemCommand Event of DataList:
protected void dListProduct_ItemCommand(object source, DataListCommandEventArgs e)
{
Label lbl = (Label)e.Item.FindControl("lblPId");
Response.Write(lbl.Text);
}
but this code never get executed.
Can anyone help me out?
Copy and paste the code below exactly the way I have it.
Code behind:
public partial class DataListExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
this.BindData();
}
}
private void BindData()
{
MyStock stock1 = new MyStock { Product_Id = 1, Stock = "Stock 1" };
MyStock stock2 = new MyStock { Product_Id = 2, Stock = "Stock 2" };
dListProduct.DataSource = new List<MyStock> { stock1, stock2 };
dListProduct.DataBind();
}
protected void dListProduct_ItemCommand(object source, DataListCommandEventArgs e)
{
Label lbl = (Label)e.Item.FindControl("lblPId");
Response.Write(lbl.Text);
}
}
public class MyStock
{
public int Product_Id { get; set; }
public string Stock { get; set; }
}
.ASPX:
<asp:DataList ID="dListProduct" runat="server" RepeatColumns="4" OnItemCommand="dListProduct_ItemCommand">
<ItemTemplate>
<div>
<table class="table-responsive" border="1">
<tr>
<td>
<asp:Label runat="server" Text="Available Stock: "></asp:Label><asp:Label ID="Label1" runat="server" Text='<%# Eval("Stock")%>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" Text="Id"></asp:Label><asp:Label ID="lblPId" runat="server" Text='<%# Eval("Product_Id")%>' Visible="false"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnAddtoCart" runat="server" Text="Add to Cart" />
</td>
</tr>
</table>
</div>
</ItemTemplate>
</asp:DataList>

Checkbox state not changed on postback

I have the following mark up for a User Control (.ascx)
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Select Logical Symbol to Search:"></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddlComponentType" runat="server"
onselectedindexchanged="ddlComponentType_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
</td>
<td>
<asp:CheckBox ID="chkAdvSearchAllLibs" runat="server" ToolTip="Check this box to search all available libraries" Text="Search All Libraries"/>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Search by Logical Symbol Properties:"></asp:Label>
</td>
<td>
</td>
</tr>
On page Load
protected void Page_Load(object sender, EventArgs e)
{
SearchResults(ref attributeSearch, compTypeID);
}
where SearchResults is
private void SearchResults(ref string attributeSearch, int compTypeID)
{
DataTable dtResults = this.AdvancedSearchControl.GetSearchResults(ref attributeSearch, compTypeID);
}
And in my UserControl.ascx.cs
public DataTable GetSearchResults(ref string _attrVals, int compTypeID)
{
//Other Logic Goes Here
IEnumerable<Model.ComponentInfo.ComponentType> compTypeResult = from compTypes in BLLibrary.GetComponentTypeBasedOnLib(this.CurrentLibraryId, this.CurrentLibrary, this.chkAdvSearchAllLibs.Checked) select compTypes.Value;
}
this.chkAdvSearchAllLibs.Checked is always false no matter if the check-box is checked on page and posted back or not.
Server side:
Add AutoPostBack="True" to the CheckBox. It's not posting back.
Client side:
<asp:CheckBox runat="server" ID="cb" onclick="checkboxchanged(this);" />
function checkboxchanged( sender ) {
if ( sender.checked ) {
// clicked and checked
} else {
// clicked and unchecked
}
}

Value of boolean cell in gridview to shown in a radio button

I have a Boolea(bit) value in database and i shown it in Grid View in the cell no. 2 like this
MyGrdView.SelectedRow.Cells[2] // for any row selected this cell have a boolean value 0 or 1
The GridView in a View 1 in Multiview control
I need in another View (view2), i can shown this value in check box, so if the boolean cell in gridview have 0, the check box checked and for 1 the check box unchecked
ASP Code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:MultiView ID="LocationsMultiView" runat="server" ActiveViewIndex="0">
<asp:View ID="View1" runat="server">
<div>
<div class="EU_TableScroll" id="showData" style="display: block">
<table class="auto-style1">
<tr>
<td>
<asp:Button ID="Btn_ShowReminders" runat="server" Text="Show Reminders" OnClick="Btn_ShowReminders_Click" />
<asp:Button ID="Btn_EditReminder" runat="server" Text="Edit Reminder" Enabled="False" OnClick="Btn_EditReminder_Click" />
<asp:Button ID="Btn_RemoveReminder" runat="server" Enabled="False" OnClick="Btn_RemoveReminder_Click" Text="Delete Reminder" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="Lbl_Error" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="Grd_Reminders" runat="server" CssClass="EU_DataTable" GridLines="Horizontal" OnSelectedIndexChanged="Grd_Reminders_SelectedIndexChanged">
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Select" HeaderText="Select" ShowHeader="True" Text=">>>" />
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl="~/img/ajax-loader.gif" />
</ProgressTemplate>
</asp:UpdateProgress>
</td>
</tr>
</table>
</div>
</div>
</asp:View>
<asp:View ID="View2" runat="server">
<table class="auto-style1">
<tr>
<td colspan="3">
<asp:Label ID="Lbl_EditRemTitle" runat="server" Text="Edit Reminder"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Lbl_Edit_Title" runat="server" Text="Title"></asp:Label>
</td>
<td>
<asp:TextBox ID="Txt_EditTitle" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ValidationGroup="g1" ControlToValidate="Txt_EditTitle">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Lbl_Edit_Content" runat="server" Text="Content"></asp:Label>
</td>
<td>
<asp:TextBox ID="Txt_Edit_Content" runat="server"></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="Lbl_Edit_Desc" runat="server" Text="Description"></asp:Label>
</td>
<td class="auto-style2">
<asp:TextBox ID="Txt_Edit_Description" runat="server" TextMode="MultiLine"></asp:TextBox>
</td>
<td class="auto-style2"> </td>
</tr>
<tr>
<td>
<asp:Label ID="Lbl_Edit_TurnOn" runat="server" Text="Turn On"></asp:Label>
</td>
<td>
<asp:CheckBox ID="CheckBox1" runat="server" />
</td>
<td> </td>
</tr>
<tr>
<td>
<asp:Label ID="Lbl_Edit_Alw" runat="server" Text="Always"></asp:Label>
</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>
<asp:Label ID="Lbl_Edit_Public" runat="server" Text="Public"></asp:Label>
</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>
<asp:Label ID="Lbl_Edit_Radius" runat="server" Text="Radius"></asp:Label>
</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="Lbl_Edit_Msg" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:UpdateProgress ID="UpdateProgress2" runat="server">
</asp:UpdateProgress>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Button ID="Button1" runat="server" Text="Save" ValidationGroup="g1" />
<asp:Button ID="Btn_Cancel_Edit" runat="server" OnClick="Btn_Cancel_Edit_Click" Text="Cancel" />
</td>
</tr>
</table>
</asp:View>
</asp:MultiView>
</ContentTemplate>
CS Code:
public partial class webusercontrols_remindersctl : System.Web.UI.UserControl
{
Locations Lo = new Locations();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Utilities.ReadFromCookie("logincookie", "user", Request) != null)
{
UserInfo UI = new UserInfo();
string userNameOnWB = Request.Cookies["logincookie"]["user"];
}
}
// Btn_ShowReminders_Click(null, null);
}
protected void Grd_Reminders_SelectedIndexChanged(object sender, EventArgs e)
{
Btn_RemoveReminder.Enabled = true;
Grd_Reminders.SelectedRow.BackColor = Color.FromName("#E56E94");
Btn_RemoveReminder.Enabled = true;
Btn_EditReminder.Enabled = true;
}
protected void Btn_ShowReminders_Click(object sender, EventArgs e)
{
Locations loc = new Locations();
string UserName = Request.Cookies["logincookie"]["user"];
Grd_Reminders.DataSource = loc.GetRemindersForUser(UserName);
Grd_Reminders.DataBind();
Grd_Reminders.SelectedIndex = -1;
Btn_RemoveReminder.Enabled = false;
Btn_EditReminder.Enabled = false;
Lbl_Error.Text = String.Empty;
}
protected void Btn_RemoveReminder_Click(object sender, EventArgs e)
{
int ID = Int32.Parse((Grd_Reminders.SelectedRow.Cells[1].Text).ToString());
if (Lo.RemoveLocations(ID))
{
Lbl_Error.Text = "Reminder Removed!";
}
else
{
Lbl_Error.Text = "Error Removing Reminder!";
}
Btn_RemoveReminder.Enabled = false;
Grd_Reminders.DataBind();
}
protected void Btn_EditReminder_Click(object sender, EventArgs e)
{
LocationsMultiView.ActiveViewIndex = 1;
if (Grd_Reminders.SelectedRow.Cells[5] != null)
Txt_EditTitle.Text = Grd_Reminders.SelectedRow.Cells[5].Text;
if (Grd_Reminders.SelectedRow.Cells[6] != null)
Txt_Edit_Content.Text = Grd_Reminders.SelectedRow.Cells[6].Text;
if (Grd_Reminders.SelectedRow.Cells[7] != null)
Txt_Edit_Description.Text = Grd_Reminders.SelectedRow.Cells[7].Text;
if (Grd_Reminders.SelectedRow.Cells[8] != null)
CheckBox1.Checked = Convert.ToBoolean(Grd_Reminders.SelectedRow.Cells[8]);
}
protected void Btn_Cancel_Edit_Click(object sender, EventArgs e)
{
LocationsMultiView.ActiveViewIndex = 0;
}
}

ASP.NET Cannot perform runtime binding on a null reference - after I'm try to login in my website

I have next controls in my ASP.NET page:
<asp:Content ID="headerPanelContent" ContentPlaceHolderID="mainContent" runat="server">
<asp:Repeater ID="rptHomePage" runat="server" DataSourceID="dsHomePage" OnItemCreated="rptHomePage_ItemCreated">
<ItemTemplate>
<div class="content-forum-section">
<table class="forum-table-view">
<tr class="content-forum-name f-background">
<td colspan="3">
<h2 class="t-color-white"><%# Eval("forumName") %></h2>
<asp:HiddenField ID="hdnForumID" Value='<%# Eval("forumID") %>' runat="server" />
</td>
</tr>
<tr class="f-background t-color-white">
<td style="width: 85%;">Section
</td>
<td style="width: 9%;">Themes
</td>
<td style="width: 9%;">Messages
</td>
</tr>
<asp:Repeater ID="rptSections" runat="server" DataSourceID="dsSectionsInForum" OnItemCreated="rptSections_ItemCreated">
<ItemTemplate>
<tr class="lightgrey-background">
<td>
<div class="forum-section-container">
<a href='<%# "./Section.aspx?id=" + Eval("SectionId").ToString() %>'><%#Eval("Name") %></a>
<br />
<asp:Repeater ID="rptSubsections" runat="server" DataSourceID="dsSubsectionsInSection">
<ItemTemplate>
<div class="subsection-link">
<a href='<%# "./Subsection.aspx?id=" + Eval("SubsectionId").ToString() %>'><%# Eval("Name") %></a>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:LinqDataSource ID="dsSubsectionsInSection" runat="server">
</asp:LinqDataSource>
</div>
</td>
<td>0
</td>
<td>0
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
<asp:LinqDataSource ID="dsSectionsInForum" runat="server"></asp:LinqDataSource>
</table>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:LinqDataSource ID="dsHomePage" runat="server"
ContextTypeName="PWO_Projekt.ForumDBDataContext"
Select="new(Id as forumID, Name as forumName)"
TableName="Forums">
</asp:LinqDataSource>
</asp:Content>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void rptHomePage_ItemCreated(object sender, RepeaterItemEventArgs e)
{
var forumDetails = (dynamic)e.Item.DataItem;
int forumID = forumDetails.forumID;
LinqDataSource lds = (LinqDataSource)e.Item.FindControl("dsSectionsInForum");
lds.ContextTypeName = "PWO_Projekt.ForumDBDataContext";
lds.TableName = "Sections";
lds.Where = "ForumId == #id";
lds.WhereParameters.Add("id", DbType.Int32, forumID.ToString());
lds.DataBind();
}
protected void rptSections_ItemCreated(object sender, RepeaterItemEventArgs e)
{
var sectionDetails = (dynamic)e.Item.DataItem;
int sectionID = sectionDetails.SectionId;
LinqDataSource lds = (LinqDataSource)e.Item.FindControl("dsSubsectionsInSection");
lds.ContextTypeName = "PWO_Projekt.ForumDBDataContext";
lds.TableName = "Subsections";
lds.Where = "SectionId == #id";
lds.WhereParameters.Add("id", DbType.Int32, sectionID.ToString());
lds.DataBind();
}
Also I have on this page my user control as login form:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="LoginForm.ascx.cs" Inherits="PWO_Projekt.Controls.LoginForm" %>
<form>
<table>
<tr>
<td>Login
</td>
<td>
<asp:TextBox ID="txtLogin" runat="server" CssClass="smallfont" Columns="15"></asp:TextBox>
</td>
<td>
<asp:CheckBox ID="chRemeber" runat="server" />
Remember me
</td>
</tr>
<tr>
<td>Password
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" CssClass="smallfont" Columns="15"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnLogin" runat="server" Text="Log in" OnClick="btnLogin_Click" />
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="lblError" runat="server" Text="" ForeColor="Red"></asp:Label>
</td>
</tr>
</table>
And code behind:
protected void btnLogin_Click(object sender, EventArgs e)
{
string login = txtLogin.Text.Trim();
string password = CommonFunctions.getMd5Hash(txtPassword.Text.Trim());
using (ForumDBDataContext db = new ForumDBDataContext())
{
db.Connection.ConnectionString = CommonFunctions.getConnectionString();
var user =
from u in db.Users
where (u.Login == login) && (u.Password == password)
select u;
if (user.Count() == 1)
{
Session["UserLogin"] = login;
Response.Redirect("./");
}
}
}
But after pressing Login button I have next error on my page:
Cannot perform runtime binding on a null reference Description: An
unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the
error and where it originated in the code.
{
var forumDetails = (dynamic)e.Item.DataItem;
int forumID = forumDetails.forumID; //error is here
LinqDataSource lds = (LinqDataSource)e.Item.FindControl("dsSectionsInForum");
lds.ContextTypeName = "PWO_Projekt.ForumDBDataContext";
And I don't understand what the problem here. This exception rises only after pressing
Login button (As I understand after PostBack)
The DataItem property is set only when you are calling DataBind() on the repeater. After a postback, the DataItem is no more present.
You should replace Item_Created, which is fired on all requests by Item_Databound which is fired when applying the databinding.

Categories

Resources