8 Replies - 2716 Views - Last Post: 27 October 2008 - 06:24 AM Rate Topic: -----

#1 AFProgMan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 21-October 08

Best Way to Edit record from front end?

Posted 24 October 2008 - 06:30 AM

Hey everyone. I am trying to add a feature to a DP app i created for a referral list. I have the Tables and add record functions working properly. I use a multiview layout to switch between the add new and edit portion. What I need to get working next is the Edit record function. I can start from scratch or just merge when I have together. What is the best way to go about populating all of the fields into the Text boxes and then save whatever changes are made.

It is an SQL database. Here is the code for the whole page. f you need any of the code behind let me know. I would like to have it with an edit button next to the delete button and then ahve it jump back to view 0 and fill in all of the fields.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="adminReferralDirectory.ascx.cs" Inherits="includes__private_adminReferralDirectory" %>
<style type="text/css">
	.style1
	{
		width: 1px;
		height: 122px;
	}
	.style2
	{
		width: 136px;
		height: 122px;
	}
	.style3
	{
		height: 122px;
	}
	.tab
	{
	}
	.tabs
	{
	}
	.text
	{
	}
	.notices
	{
	}
	.selectedTab
	{
	}
	.tabContents
	{
	}
</style>
<div>
<asp:Menu ID="Menu1" Orientation="Horizontal" StaticMenuItemStyle-CssClass="tab"
	StaticSelectedStyle-CssClass="selectedTab" CssClass="tabs" OnMenuItemClick="Menu1_MenuItemClick"
	runat="server">
	<Items>
		<asp:MenuItem Text="Add Referral" Value="0" Selected="true" />
		<asp:MenuItem Text="Edit Referrals" Value="1" />		
	</Items>
</asp:Menu>
<div class="tabContents">
<asp:MultiView ID="MultiView1" ActiveViewIndex="0" runat="server">
<asp:View ID="View1" runat="server">
<table border="0" cellpadding="0" cellspacing="2" width="600">
 <tr>
	<td style="width: 1px">
		<span style="color: #ff0000">*</span></td>
	<td style="width: 136px">
		<asp:Label ID="lblRefFirstName" runat="server" CssClass="text" Text="Referral First Name"></asp:Label></td>
	<td>
		<asp:TextBox ID="txtRefFirstName" runat="server" Width="279px"></asp:TextBox>&nbsp;
		<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtRefFirstName"
			Display="Dynamic" ErrorMessage="Required Field!" SetFocusonerror="True" ValidationGroup="Referrals"></asp:RequiredFieldValidator></td>
</tr>
<tr>
	<td style="width: 1px">
		<span style="color: #ff0000">*</span></td>
	<td style="width: 136px">
		<asp:Label ID="lblRefLastName" runat="server" CssClass="text" Text="Referral Last Name"></asp:Label></td>
	<td>
		<asp:TextBox ID="txtRefLastName" runat="server" Width="279px"></asp:TextBox>&nbsp;
		<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtRefLastName"
			Display="Dynamic" ErrorMessage="Required Field!" SetFocusonerror="True" ValidationGroup="Referrals"></asp:RequiredFieldValidator></td>
</tr>
<tr>
	<td style="width: 1px; height: 24px;">
		<span style="color: #ff0000">*</span></td>
	<td style="width: 136px; height: 24px;">
		<asp:Label ID="lblRefFirmName" runat="server" CssClass="text" Text="Referral Firm Name"></asp:Label></td>
	<td style="height: 24px">
		<asp:TextBox ID="txtRefFirmName" runat="server" Width="279px"></asp:TextBox>&nbsp;
		<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtRefFirmName"
			Display="Dynamic" ErrorMessage="Required Field!" SetFocusonerror="True" ValidationGroup="Referrals"></asp:RequiredFieldValidator></td>
</tr>
<tr>
	<td style="width: 1px">
		<span style="color: #ff0000">*</span></td>
	<td style="width: 136px">
		<asp:Label ID="lblRefSpecialty" runat="server" CssClass="text" Text="Referral Specialty"></asp:Label></td>
	<td>
		<asp:TextBox ID="txtRefSpecialty" runat="server" Width="279px"></asp:TextBox>&nbsp;
		<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtRefSpecialty"
			Display="Dynamic" ErrorMessage="Required Field!" SetFocusonerror="True" ValidationGroup="Referrals"></asp:RequiredFieldValidator></td>
</tr>
<tr>
	<td style="width: 1px">
	 <span style="color: #ff0000">*</span></td>
	<td style="width: 136px">
		<asp:Label ID="lblRefAddress" runat="server" CssClass="text" Text="Referral Address"></asp:Label></td>
	<td>
		<asp:TextBox ID="txtRefAddress" runat="server" Width="279px"></asp:TextBox>&nbsp;
		 <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="txtRefAddress"
			Display="Dynamic" ErrorMessage="Required Field!" SetFocusonerror="True" ValidationGroup="Referrals"></asp:RequiredFieldValidator>
	</td>
</tr>
<tr>
	<td style="width: 1px">
		<span style="color: #ff0000">*</span></td>
	<td style="width: 136px">
	<asp:Label ID="lblRefCity" runat="server" CssClass="text" Text="Referral City"></asp:Label></td>
	<td>
		<asp:TextBox ID="txtRefCity" runat="server" Width="279px"></asp:TextBox>&nbsp;
		<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtRefCity"
			Display="Dynamic" ErrorMessage="Required Field!" SetFocusonerror="True" ValidationGroup="Referrals"></asp:RequiredFieldValidator></td>
</tr>
<tr>
	<td style="width: 1px">
		<span style="color: #ff0000">*</span></td>
	<td style="width: 136px">
		<asp:Label ID="lblRefState" runat="server" CssClass="text" Text="Referral State"></asp:Label></td>
	<td>
		<asp:DropDownList ID="cboStates" runat="server" Width="277px" DataTextField="State"
			DataValueField="Abbrev" />&nbsp;
		<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="cboStates"
			Display="Dynamic" ErrorMessage="Required Field!" SetFocusonerror="True" ValidationGroup="Referrals"></asp:RequiredFieldValidator></td>
</tr>
<tr>
	<td style="width: 1px">
	<span style="color: #ff0000">*</span>
	</td>
	<td style="width: 136px">
		<asp:Label ID="lblRefPostalCode" runat="server" CssClass="text" Text="Referral Postal Code"></asp:Label></td>
	<td>
	<asp:TextBox ID="txtRefPostalCode" runat="server" Width="279px"></asp:TextBox>&nbsp;
	<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="txtRefPostalCode"
		Display="Dynamic" ErrorMessage="Required Field!" SetFocusonerror="True" ValidationGroup="Referrals"></asp:RequiredFieldValidator>
	</td>
</tr>
<tr>
	<td style="width: 1px; height: 24px">
		</td>
	<td style="height: 24px; width: 136px;">
		<asp:Label ID="lblRefPhoneNumber" runat="server" CssClass="text" Text="Referral Phone #"></asp:Label></td>
	<td style="height: 24px">
		<asp:TextBox ID="txtRefPhoneNumberAreaCode" runat="server" Width="51px"></asp:TextBox>&nbsp;
		<asp:TextBox ID="txtRefPhoneNumber" runat="server" Width="219px"></asp:TextBox>&nbsp;
	   </td>
</tr>
<tr>
	<td style="width: 1px; height: 24px">
	   </td>
	<td style="height: 24px; width: 136px;">
		<asp:Label ID="lblRefFaxNumber" runat="server" CssClass="text" Text="Referral Fax #"></asp:Label></td>
	<td style="height: 24px">
		<asp:TextBox ID="txtRefFaxNumberAreaCode" runat="server" Width="51px"></asp:TextBox>&nbsp;
		<asp:TextBox ID="txtRefFaxNumber" runat="server" Width="219px"></asp:TextBox>&nbsp;
		</td>
</tr>
<tr>
	<td style="width: 1px">
	</td>
	<td style="width: 136px">
		<asp:Label ID="lblRefEmailAddress" runat="server" CssClass="text" Text="Referral Email Address"></asp:Label></td>
	<td>
	<asp:TextBox ID="txtRefEmailAddress" runat="server" Width="279px"></asp:TextBox>
	</td>
</tr>
<tr>
	<td style="width: 1px">
	</td>
	<td style="width: 136px">
		<asp:Label ID="lblRefWebsite" runat="server" CssClass="text" Text="Referral Website"></asp:Label></td>
	<td>
	<asp:TextBox ID="txtRefWebsite" runat="server" Width="279px"></asp:TextBox>
	</td>
</tr>
 <tr>
	<td class="style1">
	</td>
	<td class="style2">
	<asp:Label ID="Label2" runat="server" CssClass="text" Text="Referral Description"></asp:Label>
		&nbsp;</td>
	<td class="style3">
	<asp:TextBox ID="TxtRefDescription" runat="server" Width="279px" Height="112px" 
			Rows="4" TextMode="MultiLine"></asp:TextBox>
	</td>
</tr>
<tr>
	<td style="width: 1px">
	</td>
	<td style="width: 136px">
	</td>
	<td>
		&nbsp;</td>
	</tr>
<tr>
	<td style="width: 1px">
	</td>
	<td style="width: 136px">
		<asp:Label ID="hidstatus" runat="server" CssClass="text" Visible="False"></asp:Label></td>
	<td>
		<asp:Button ID="cmdSubmitRef" runat="server" Width="188px" onclick="cmdSubmitRef_Click"
			Text="Submit Referral Information" ValidationGroup="Referrals" />&nbsp;
		<asp:Label ID="lblUserMessage" runat="server" ForeColor="Red"></asp:Label>
	</td>
	</tr>
	<tr>
		<td style="width: 1px">
		</td>
		<td style="width: 136px">
		</td>
		<td>
		</td>
	</tr>
	<tr>
		<td colspan="3" rowspan="2">
			<span class="notices" style="color: #ff0000">* Fields are required.<br />Please complete all required fields before submitting.</span>
	</td>
	</tr>
</table>
</asp:View>
<asp:View ID="View2" runat="server">
	<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
		AutoGenerateColumns="False" EmptyDataText="No referrals in the database." 
		PageSize="50" SkinID="gvClearSkin" Width="600px" OnRowDataBound="GridView1_RowDataBound"  
		onrowdeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting">
		<Columns>
		<asp:BoundField DataField="refid" Visible="False" />
		<asp:BoundField DataField="reffirstname" HeaderText="First Name" />
		<asp:BoundField DataField="reflastname" HeaderText="Last Name" />
		<asp:HyperLinkField DataNavigateUrlFields="refid" 
			DataNavigateUrlFormatString="~\admin\default.aspx?ld=refDirectory&amp;refid={0}" 
			DataTextField="reffirmname" HeaderText="Firm Name" />
		<asp:BoundField DataField="refspecialty" HeaderText="Specialty" />
		<asp:BoundField DataField="refstate" HeaderText="State" />   
	<asp:TemplateField  HeaderText="Select">		 
		<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("refid") %>' CommandName="Delete" 
runat="server">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
		</Columns>
	</asp:GridView>
</asp:View>
</asp:MultiView>
</div>
</div>




using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class includes__private_adminReferralDirectory : System.Web.UI.UserControl
{
	private int deleteid = data.intRefID;
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			cboStates.DataSource = functions.GetStates();
			cboStates.DataBind();
			cboStates.Items.Insert(0, "Please Select");		  
		}

		if (data.intRefID != 0)
		{
			bindReferrals();
		}
	}

	protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
	{
		int index = Int32.Parse(e.Item.Value);

		if ((index == 0) || (index == 1))
			MultiView1.ActiveViewIndex = index;

		switch (index)
		{ 
			case 0:
				cleanTextBoxes();
				break;
			case 1:
				GridView1.DataSource = directorylistings.GetDirectoryListings();
				GridView1.DataBind();
				break;
		}
	}

	protected void cmdSubmitRef_Click(object sender, EventArgs e)
	{	   
		string firstname = txtRefFirstName.Text.Trim();
		string lastname = txtRefLastName.Text.Trim();
		string firmname = txtRefFirmName.Text.Trim();
		string specialty = txtRefSpecialty.Text.Trim();
		string address = txtRefAddress.Text.Trim();
		string city = txtRefCity.Text.Trim();
		string state = cboStates.SelectedItem.Value.Trim();
		string postalcode = txtRefPostalCode.Text.Trim();
		string phoneareacode = txtRefPhoneNumberAreaCode.Text.Trim();
		string phonenumber = txtRefPhoneNumber.Text.Trim();
		string faxareacode = txtRefFaxNumberAreaCode.Text.Trim();
		string faxnumber = txtRefFaxNumber.Text.Trim();
		string emailaddy = txtRefEmailAddress.Text.Trim();
		string website = txtRefWebsite.Text.Trim().Replace("http://", "");
		string description = TxtRefDescription.Text.Trim();

		if (directorylistings.Insert(firstname, lastname, firmname, specialty, address, city, state, postalcode, phoneareacode, phonenumber, faxareacode, faxnumber, emailaddy, website, description) == true)
		{
			lblUserMessage.Text = "Referral added to the database.";
			cleanTextBoxes();
		}
		else
		{
			lblUserMessage.Text = "This request resulted in an error.";
		}	   
	}

	private void cleanTextBoxes()
	{
		txtRefFirstName.Text = string.Empty;
		txtRefLastName.Text = string.Empty;
		txtRefFirmName.Text = string.Empty;
		txtRefSpecialty.Text = string.Empty;
		txtRefAddress.Text = string.Empty;
		txtRefCity.Text = string.Empty;				 
		cboStates.SelectedIndex = 0;
		txtRefPostalCode.Text = string.Empty;
		txtRefPhoneNumberAreaCode.Text = string.Empty;
		txtRefPhoneNumber.Text = string.Empty;
		txtRefFaxNumberAreaCode.Text = string.Empty;
		txtRefFaxNumber.Text = string.Empty;
		txtRefEmailAddress.Text = string.Empty;
		txtRefWebsite.Text = string.Empty;
		TxtRefDescription.Text = string.Empty;
	}

	private void bindReferrals()
	{
		foreach (DataRow dr in directorylistings.GetReferralByID(data.intRefID).Tables[0].Rows)
		{
			txtRefFirstName.Text = dr["reffirstname"].ToString();
			txtRefLastName.Text = dr["reflastname"].ToString();
			txtRefFirmName.Text = dr["reffirmname"].ToString();
			txtRefSpecialty.Text = dr["refspecialty"].ToString();
			txtRefAddress.Text = dr["refaddress"].ToString();
			txtRefCity.Text = dr["refcity"].ToString();	

			try
			{
				cboStates.ClearSelection();
				cboStates.Items.FindByValue(dr["refstate"].ToString()).Selected = true;
			}
			catch { }

			txtRefPostalCode.Text = dr["refzipcode"].ToString();
			txtRefPhoneNumberAreaCode.Text = dr["refphoneareacode"].ToString();
			txtRefPhoneNumber.Text = dr["refphonenumber"].ToString();
			txtRefFaxNumberAreaCode.Text = dr["reffaxareacode"].ToString();
			txtRefFaxNumber.Text = dr["reffaxnumber"].ToString();
			txtRefEmailAddress.Text = dr["refemailaddress"].ToString();
			txtRefWebsite.Text = dr["refwebsiteaddress"].ToString();
			TxtRefDescription.Text = dr["refdescription"].ToString();
		}
	}

	protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
			l.Attributes.Add("onclick", "java script:return " +
			"confirm('Are you sure you want to delete referral #" +
			DataBinder.Eval(e.Row.DataItem, "refid") + "?')");

			deleteid = (int)DataBinder.Eval(e.Row.DataItem, "refid");			
		}
	}

	protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
	{	 
	}

	protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
	{	
	}

	protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
	{
		directorylistings.DeleteReferralByID(deleteid);
	}
}


And that is the code behind.

Thank you for even taking a look at this.

Is This A Good Question/Topic? 0
  • +

Replies To: Best Way to Edit record from front end?

#2 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: Best Way to Edit record from front end?

Posted 24 October 2008 - 06:58 AM

I think this is ASP.NET.

If it is Post it in the ASP.NET Forum. It will get the right attention over there.
Was This Post Helpful? 0
  • +
  • -

#3 AFProgMan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 21-October 08

Re: Best Way to Edit record from front end?

Posted 24 October 2008 - 07:08 AM

Thanks. It is a .net site programmed in C# so i never know where to put these topics. Can I move it?
Was This Post Helpful? 0
  • +
  • -

#4 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: Best Way to Edit record from front end?

Posted 24 October 2008 - 07:13 AM

No just wait for a moderator to move it. If it hasn't been moved by tomorrow you can try to PM a moderator.
Was This Post Helpful? 0
  • +
  • -

#5 AFProgMan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 21-October 08

Re: Best Way to Edit record from front end?

Posted 24 October 2008 - 08:02 AM

Maybe I will just close discussion and start it new in ASP.Net forums? No reason to waste everyone's time.
Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Best Way to Edit record from front end?

Posted 24 October 2008 - 08:29 AM

Moved to ASP.NET.

I am not sure if I understand exactly what you want. But if you are going to populate textboxes with the values from your table. First determine which row was selected in the GridView, query the DB to get the data relating to that record via the primary key and then populate the textboxes with the data. When you have finished, perform an update on that record using the values entered into the textboxes.

1) Do you know how to query the DB to get a specific record?
2) Do you know how to update a row in SQL Server?

On a side not, all of this functionality is already built into the GridView, you just need to enable it.

Here is a couple of tutorials if you are interested in going that route.
Insert, Update, Delete with Gridview
Simple Insert, Select, Edit, Update and Delete in Asp.Net GridView control

You could also tie the GridView to a DetailsView control and edit the records that way.
GridView-DetailsView (Master/Detail) Control
Was This Post Helpful? 0
  • +
  • -

#7 AFProgMan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 21-October 08

Re: Best Way to Edit record from front end?

Posted 24 October 2008 - 08:37 AM

Awesome, thanks for moving the thread, that I put in the wrong section and also thanks for the info. I Do no SQL queries and update commands. Sometimes I have trouble laying them down on the site I need.
Was This Post Helpful? 0
  • +
  • -

#8 AFProgMan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 21-October 08

Re: Best Way to Edit record from front end?

Posted 24 October 2008 - 10:37 AM

Is it possible to query the info and set it to those text fields already created and and used in VIEW1? Or should I make a 3rd View and try to create an entire edit function based on that view? Would it be easier to control the commands from a 3rd view or more difficult? Right now I added a link next to the 'delete' link called 'edit' that I am hoping to click on and have all of the row info for that record populated for editing.

Anybody have any ideas on best way to go about this particular set up?
Was This Post Helpful? 0
  • +
  • -

#9 AFProgMan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 21-October 08

Re: Best Way to Edit record from front end?

Posted 27 October 2008 - 06:24 AM

Bump... Thinking I am going to need a 3rd view.
And populate a dropdown list with all of the referral names and select it that way?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1