How to bind to textbox and retrieve modified value on button click.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Sku objSku = new Sku();
objSku.Name = "Test";
Sku = objSku;
frm.DataSource = Sku;
frm.DataBind();
}
if (IsPostBack)
{
Sku = ViewState["Sku"] as Sku;
}
}
public Sku Sku { get; set; }
protected void Button1_Click(object sender, EventArgs e)
{
//Hers sku.Name should be equal to the value entered by the user . so that I can save the object.
}
}
[Serializable]
public class Sku
{
public string Name { get; set; }
}
Html code
<form id="form1" runat="server">
<div>
<asp:FormView runat="server" ID="frm" >
<ItemTemplate>
</ItemTemplate>
</asp:FormView>
<asp:TextBox ID="TextBox1" Text='<%# Bind("Name") %>' runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
You have direct access to your textbox as it's not in the FormView so:
String val= Textbox1.Text;
Textbox1.Text="what ever you are binding?"
I think you may need to be a little clear in your question as I don't understand what you are trying to do?
It looks like you're already binding to the field for displaying data. Is this not working? Within your button click event handler, you can retrieve the current value from TextBox1.Text.
If you want to update your view on postback you need to update the bindings.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Sku objSku = new Sku();
objSku.Name = "Test";
Sku = objSku;
}
if (IsPostBack)
{
Sku = ViewState["Sku"] as Sku;
}
frm.DataSource = Sku;
frm.DataBind();
}
Related
For school project, i am assigned to make a simple login form with c# or asp.net. I use Response.Redirect() but whenever i click the button, it only refreshes the login page and does not direct to another page. Can someone help me?
This is the source code for the login button in login_page.aspx:
<asp:Button ID="btnLogin" runat="server" Height="34px" Text="Login" Width="102px" OnServerClick="btnLogin_Click"/>
and this is the code behind:
using System;
using System.Web;
using System.Data;
using System.Text;
namespace WebApplication1
{
public partial class LoginPage : System.Web.UI.Page
{
public string txtUserName { get; private set; }
public string txtPassword { get; private set; }
public object lblPassword { get; private set; }
public object lblUsername { get; private set; }
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
if (txtUserName == "admin" && txtPassword == "password")
{
Response.Redirect(url: "admin_page.aspx");
}
else
{
Response.Redirect(url: "not_page.aspx");
}
}
}
}
If you are using asp:Button control, it does not have any event called OnServerClick. It is associated with HTML Button
asp:Button control has OnClick for handling server side click event and OnClientClick for handing client side click event.
Hope this clarifies.
Try this. It should be onClick event.
<asp:Button ID="btnLogin" runat="server" Height="34px" Text="Login" Width="102px" onClick="btnLogin_Click"/>
protected void btnLogin_Click(object sender, EventArgs e)
{
if (txtUserName == "admin" && txtPassword == "password")
{
Response.Redirect("~/admin_page.aspx");
}
else
{
Response.Redirect("~/not_page.aspx");
}
}
Response.Redirect("~/admin_page.aspx");
remove: url: and add relative path ~/
Or use PostBackUrl in .aspx file.
<asp:Button ID="btnLogin" runat="server" Height="34px" Text="Login" Width="102px" OnServerClick="btnLogin_Click" PostBackUrl="~/admin_page.aspx" />
I'm trying to use ASPxGridView to display a list of ASPxComboBox controls. Both the rows in the grid and the list of options in the combo boxes are populated from code. I'm having problems setting the initial value of the combo boxes.
I'm looking for it to look similar to the image below.
As you can see in the screenshot, I have been able to get both the grid view & the combo boxes to populate, but I can't figure out how to set the initial values of the combo boxes.
In the Init event of the inner combo boxes, there's no obvious property to retrieve the bound object.
I did find a couple other questions on StackOverflow, for which the answer was to add a bound property to the combo box. However, adding SelectedIndex='<%# Bind("Level") %>' to the declaration for InnerCombo gave me the error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."
Here's what I have so far:
Testing.aspx:
<%# Page Title="" Language="C#" MasterPageFile="~/Light.master"
AutoEventWireup="true" CodeBehind="Testing.aspx.cs" Inherits="MyProject.Testing" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<dx:ASPxGridView ID="MyGridView" runat="server" KeyFieldName="Name">
<Columns>
<dx:GridViewDataColumn FieldName="Name" />
<dx:GridViewDataColumn FieldName="Level">
<DataItemTemplate>
<dx:ASPxComboBox
runat="server"
ID="InnerCombo"
ValueField="ID"
TextField="Desc"
ValueType="System.Int32"
OnInit="InnerCombo_Init" />
</DataItemTemplate>
</dx:GridViewDataColumn>
</Columns>
</dx:ASPxGridView>
<dx:ASPxButton runat="server" ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" />
</asp:Content>
Testing.aspx.cs:
public partial class Testing : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.MyGridView.DataSource = GetDefaultSettings();
this.MyGridView.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Debug.WriteLine("btnSubmit_Click");
for (int i = 0; i < MyGridView.VisibleRowCount; i++)
{
object[] row = (object[])MyGridView.GetRowValues(i, "Name", "Value");
// row[1] is null, but we can get the data by finding the combo box itself.
GridViewDataColumn col = (GridViewDataColumn)MyGridView.Columns["Value"];
ASPxComboBox innerCombo = (ASPxComboBox)MyGridView.FindRowCellTemplateControl(i, col, "InnerCombo");
Debug.WriteLine("{0} = {1}", row[0], innerCombo.Value);
}
}
protected void InnerCombo_Init(object sender, EventArgs e)
{
Debug.WriteLine("InnerCombo_Init");
ASPxComboBox innerCombo = sender as ASPxComboBox;
if (innerCombo != null)
{
innerCombo.DataSource = GetValues();
innerCombo.DataBind();
}
}
private static List<ValueClass> GetValues()
{
// Simple for testing; actual method will be database access.
return new List<ValueClass>()
{
new ValueClass(0, "Zero (default)"),
new ValueClass(1, "One"),
new ValueClass(2, "Two"),
new ValueClass(3, "Three"),
};
}
private static List<SettingClass> GetDefaultSettings()
{
// Simple for testing; actual method will be database access + post-processing.
return new List<SettingClass>()
{
new SettingClass("AA", 0),
new SettingClass("BB", 1),
new SettingClass("CC", 0),
};
}
public class ValueClass
{
public int ID { get; private set; }
public string Desc { get; private set; }
public ValueClass(int id, string desc)
{
this.ID = id;
this.Desc = desc;
}
}
public class SettingClass
{
public string Name { get; set; }
public int Value { get; set; }
public SettingClass(string name, int value)
{
this.Name = name;
this.Value = value;
}
}
}
I have WebUserControl with DataBind override:
public partial class WebUserControl1 : System.Web.UI.UserControl
{
public object DataSource { get; set; }
public string Text { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
}
public override void DataBind()
{
// *** there when called, properties are null, why? ***
repeater2.DataSource = DataSource;
repeater2.DataBind();
}
}
This control is in repeater with declarative bounded properties:
<asp:Repeater ID="repeater" runat="server">
<ItemTemplate>
<WebUserControl1 runat="server" DataSource='<%# DataBinder.Eval(Container.DataItem, "levels") %>' Text='<%# DataBinder.Eval(Container.DataItem, "Text") %>' />
</ItemTemplate>
</asp:Repeater>
Now when i call DataBind() on repeater:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
... data query
repeater.DataSource = items;
repeater.DataBind();
}
}
in overriden control's DataBind() method i don't have properly setted properties DataSource and Text, they are null, why?
If you have only one repeater control in WebUserControl1, it is easier to bind that child repeater control in Repeater.ItemDataBound Event of Parent repeater.
Here is the sample -
ASPX
<asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="ParentRepeater_ItemDataBound">
<ItemTemplate>
<asp:Label runat="server" ID="StreetLabel" /><br/>
<asp:Repeater ID="ChildRepeater" runat="server">
<ItemTemplate>
<asp:Label runat="server" ID="FirstNameLabel" />
<asp:Label runat="server" ID="LastNameLabel" />
</ItemTemplate>
</asp:Repeater><hr/>
</ItemTemplate>
</asp:Repeater>
Code Behind
public class Address
{
public int Id { get; set; }
public string Street { get; set; }
public List<User> Users { get; set; }
}
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ParentRepeater.DataSource = new List<Address>
{
new Address
{
Id = 1,
Street = "1st Street",
Users = new List<User>()
{
new User {Id = 1, FirstName = "John", LastName = "Doe"},
new User {Id = 1, FirstName = "Marry", LastName = "Doe"}
}
},
new Address
{
Id = 2,
Street = "2nd Street",
Users = new List<User>()
{
new User {Id = 1, FirstName = "Eric", LastName = "Newton"},
new User {Id = 1, FirstName = "John", LastName = "Newton"}
}
}
};
ParentRepeater.DataBind();
}
}
protected void ParentRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var address = e.Item.DataItem as Address;
var streetLabel = e.Item.FindControl("StreetLabel") as Label;
streetLabel.Text = address.Street;
var repeater = e.Item.FindControl("ChildRepeater") as Repeater;
repeater.ItemDataBound += ChildRepeater_ItemDataBound;
repeater.DataSource = address.Users;
repeater.DataBind();
}
}
protected void ChildRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var user = e.Item.DataItem as User;
var firstNameLabel = e.Item.FindControl("FirstNameLabel") as Label;
firstNameLabel.Text = user.FirstName;
var lastNameLabel = e.Item.FindControl("LastNameLabel") as Label;
lastNameLabel.Text = user.LastName;
}
}
Solution for Original Question
Delete public override void DataBind() event and bind repeaters2 inside PreRender event.
public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected override void OnPreRender(EventArgs e)
{
repeater2.DataSource = DataSource;
repeater2.DataBind();
base.OnPreRender(e);
}
}
I'm using a gridview that I want to be able to click on each row to be able to display another field from the object that I'm displaying. It feels like it's easy to solve but maybe I'm stupid because I can't find it anywhere...
The ASP-code:
<asp:GridView ID="gvMessages" runat="server" AutoGenerateColumns = "false"
CaptionAlign="NotSet" CellPadding="5">
<Columns>
<asp:BoundField HeaderText="Avsändare" DataField="Sender" />
<asp:BoundField HeaderText="Ämne" DataField="Head" />
</Columns>
</asp:GridView>
Code-Behind:
protected void Page_Load(object sender, EventArgs e)
{
gvMessages.DataSource = con.GetMails(con.GetId(Membership.GetUser().UserName));
gvMessages.DataBind();
}
Not sure that all this is necessery for the problem but here is the method in my wcf-project that is filling my composite class with object info
public List<MailInfo> GetMails(int id)
{
using (var client = new datingEntities())
{
var result = client.Mail.Where(x => x.SentTo == id).Select(x => new MailInfo
{
Message = x.Mail1,
Reciever = x.SentTo,
Read = (bool)x.IsRead,
Sender = (int)x.SentFrom,
Head = x.Subject
}).ToList();
return result;
}
}
Composite-class:
[DataContract]
public class MailInfo : Mail
{
[DataMember]
public string Message { get; set; }
[DataMember]
public int Reciever { get; set; }
[DataMember]
public bool Read { get; set; }
[DataMember]
public int Sender { get; set; }
[DataMember]
public string Head { get; set; }
}
You should databind the GridView only if(!Page.IsPostBack).
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
gvMessages.DataSource = con.GetMails(con.GetId(Membership.GetUser().UserName));
gvMessages.DataBind();
}
}
If you want to select a row on click you can use javascript:
protected void gvMessages_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Row.ToolTip = "Click to select row";
e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
}
}
Now you can handle the SelectedIndexChangedEvent whenever the user clicks somewhere in the row:
protected void gvMessages_SelectedIndexChanged(Object sender, EventArgs e)
{
// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;
}
You should use the OnSelectedIndexChanged event.
<asp:GridView ID="gvMessages" runat="server" AutoGenerateColumns = "false"
OnSelectedIndexChanged="gvMessages_SelectedIndexChanged"
CaptionAlign="NotSet" CellPadding="5">
<Columns>
<asp:BoundField HeaderText="Avsändare" DataField="Sender" />
<asp:BoundField HeaderText="Ämne" DataField="Head" />
</Columns>
</asp:GridView>
Then in the event's definition, you can retrieve the selected item and do whatever you need from there.
protected void gvMessages_SelectedIndexChanged(object sender, EventArgs e)
{
if (ContactsGridView.SelectedIndex >= 0)
ViewState["SelectedKey"] = gvMessages.SelectedValue;
else
ViewState["SelectedKey"] = null;
}
Example from the MSDN official documentation.
I'm using asp.net dynamic data. There is a field I want to auto fill for users. The file is user name which can be obtained with integrated windows authentication.
I created a Field Template and it works in Insert mode, but in Update mode, once another user updates the record, the new user name should be saved, but it didn't.
I found a solution and I want to share it.
Apply UIHint for the entity field:
[UIHint("UserName")]
public object Name { get; set; }
Create a a new FieldTemplate:
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="true" BorderStyle="None"
BackColor="Transparent" Text='<%# FieldValueEditString %>'></asp:TextBox>
Code behind for FieldTemplate:
public partial class UserName_EditField : FieldTemplateUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
SetUpValidator(RequiredFieldValidator1);
EditFieldTemplateUserControl.InsertHelpText(this);
}
protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
TextBox1.Visible = true;
}
protected string UserName
{
get
{
var name = this.Page.User.Identity.Name.ToString();
return name;
}
}
protected override void ExtractValues(IOrderedDictionary dictionary)
{
if (Page.IsPostBack)
{// only assign value when user posts back.
this.TextBox1.Text = this.UserName.ToUpper();
dictionary[Column.Name] = ConvertEditedValue(TextBox1.Text);
}
}
public override Control DataControl
{
get
{
return TextBox1;
}
}
}