Category Archives: FormView

[RESOLVED] DropDownList value case

I have a DropDownList control that I am filling the selections in code and set the value to the value on the FormView Datasource row.  However, if the case is different it does not set the selectedvalue to the current row value.  It seems to be the case of the text because the table used to fill the dropdownlist has Jeep for a value and the data row has JEEP for a value.  How can I make the DropDownList case insensitive?  Thanks.

You can compare either UPPER to UPPER case or ignore case when searching for the value.

if (string.Equals("Your DropDownList value here""Your FormView Field Value here"StringComparison.InvariantCultureIgnoreCase))
{
    // select FormView row here
}

use String.ToUpper() methon for converting the lower case strings to upper case...

http://msdn.microsoft.com/en-us/library/ewdd6aed.aspx

Thanks.

[RESOLVED] Bind to Custom Control

Hi,

Anybody know how to bind the data to custom control.

ex. i had create a custom control that consist of a TextBox and a Button. how do i tell the aspx to bind the data to the TextBox in custom control like in usual aspx

<asp:TextBox ID="AcNoTextBox" runat="server" Text='<%# Bind("AcNo") %>' />

Thanks,

Andy

<div>

Hi Andy,

As per my knowledge you are creating Coposite DataBound Control am i right ?

If you are creating  compositeDataBound Control menas you need expose DataTextField and DataValueField , and this control is having CreateChildControl(System.Collections.IEnumerable dataSource, bool dataBinding)

{

// need to bind here

}

In above method you need to get the Dag taTextFiled and try to attach the respective value to TextBox.Text property.

If you can explain little bit extra about you control then i will explain clearly .

 

 

 

</div>

Hi kanthaa,

you are right, it's a composite control. this composite control will have additional function rather than usual textbox and i will put this composite control within Formview/GridView, replacing one of the textbox field. all function within this composite control has been worked as expected, but i just don't know how to make it bind like Text='<%# Bind("AcNo") %>'. can you show me the example.

thanks,

Andy

hi NamrataC,

thanks for the link, but i am still a little bit confused. to summary it up.

i only need to assign DataSource, DataTextField, DataValueField to the text control in my custom control, and issue DataBind in CreateChildControls event, right ?

Thanks,

Andy

 

Hi andysujoto,

As far as I know, if you want to use the bind method for the textbox, you should put the textbox into the template.

Thus you use the bind the to resolve the data of parent control template.

Please check the following link:

http://msdn.microsoft.com/en-us/library/ms178366.aspx

http://support.microsoft.com/kb/307860

Hi Hua,

Can you more specific?

Let's put it in example,the usual in formview is like in below:

<asp:FormView ID="FormView1"
DataSourceID="SqlDataSource1"
DataKeyNames="AcNo"
RunAt="server">
<EditItemTemplate>
<table>
<tr>
<td align="right"><b>Account No:</b></td>
<td><asp:TextBox ID="AcNoTextBox" RunAt="Server" Text='<%# Bind("AcNo") %>' />
</td>
</tr>
<tr>
<td align="right"><b>Account Name:</b></td>
<td><asp:TextBox ID="AcNmTextBox" RunAt="Server" Text='<%# Bind("AcNm") %>' />
</td>
</tr>
<tr>
<td align="right"><b>Account Head:</b></td>
<td><asp:TextBox ID="AcHeadTextBox" RunAt="Server" Text='<%# Bind("AcHead") %>' />
</td>
</tr>

</table>
</EditItemTemplate>
</asp:FormView>

then i am thinking of creating a lookup function which is a compositecontrol (that has a TextBox and a Button),
The Textbox (in compositecontrol) will function to receive value returned from Button Click (which is worked as expected already)
and this textbox will also function as two-way binding (this one i am asking). in this case, to replace AcHeadTextBox.

Hopefully its clear to you.

Thanks,
Andy

Hiandysujoto,

Based on my experience, if you want to use the 'bind' for the textbox, you should ensure the parent control is template control and have its datasource.

You should create a similar control like the formview, that is to say, the parent control inherit some interface.

Thus you can use the bind method.

http://weblogs.asp.net/scottgu/archive/2005/12/04/432319.aspx

Hi Hua,

The link within the link you given was not not worked as expected. it's already deleted ?

Regards,

Andy

[RESOLVED] Dynamically populating a page

Hi Guys,

I'm currently building my website and have hit a brick wall. I've not begun coding anything fro this yet as I am unable to get my head around it and not sure where to start.

I've got a database table with a list of town/ county names ( columns are id, town, county).

What I'd like to do is have a template page with set places for town/ county names and then when a particular area is searched for in google/ bing etc my template page with the relevant town/ county name is shown.

I know that's a bit vague so if you have any questions, let me know. One thing I don't want to do is manually create a page for each individual town/ county! :-)

Thanks in advance!

Based on the info you provided this is a long shot.
But why create a template for each town/country.
If the info is from the database the template should be based upoon the metadata you provided.

So your template would contain the the reference! to the town/country in the database.

Which means you only have one template. Or possible only one databound control likie formview,GridView,Listview dependingon how and what you want to show.

Now as foor Google info the same approach counts here. You need to use google's api to pass info through to google maps f.i. to retrieve the location of the item on teh map.

 

I am totally confused....:-)

Hi Guys,

Thanks for getting back to me.

I think my problem was that I was trying to make it too complicated.

Anyway, once I realised that, I found that it was actually quite simple.

I've got a template that gets called and then, using a query string, the relevant location populates set parts of the text.

Simples :-)

[RESOLVED] Gridview getting out of synch with source data

The Patient Scheduling system I have built extracts current inpatient data from an enterprise health system and merges it with data from a seperate database, based on a common key. This is then displayed in a gridview control for browse/update.

At any one point in time, the client list in my system should reflect the current inpatients in the enterprise system.
The problem is that if the user has not refreshed the client list in some time, there may have been a discharge in the enterprise system and the two get out of synch. When the user selects a client from the list to update, the details of an entirely different client (usually the one below), is presented in edit mode. e.g. if the user selects  the 4th client in the list, the details for the 5th client are made available for update. 

Any thoughts on how I can get around this.

One option would be to not edit within the GridView itself, and use the DetailsView / FormView combination (with or without a ModalPopupExtender if you will, to make it "user-friendly").
But that will ensure the GridView doesn't bind over its data source again. You would use the RowCommand or SelectedIndexChanged events.
Even though the details might load with changes, IF any applied by someone else in the meantime, which might be quite different to those displayed in the GridView row.

 

Another option might be to find the RowIndex that matches the value by ID (DataKeys) chosen to be edited (besides, this might be on anther page entirely). 

 

 

Madog

e.g. if the user selects  the 4th client in the list, the details for the 5th client are made available for update. 

looks like, in the meantime, somebody else deleted a record

 

Madog

The problem is that if the user has not refreshed the client list in some time,

I think it's a tall order to expect the GridView to take responsibility to sync-up over such unusually long time periods (or any other control for that matter).

(You could have your users refresh the GridView if they've been away too long! ;))

Thanks for your replies.

I understand its not the gv's fault, but just require a workaround. The gv is contained within an AJAX udpatepanel, so the approach I am taking at the moment is to assess the performance impact of databinding the gv on a regular interval using a timer.

If that doesn't work out, I plan to extract the key field in rowcommand (which takes place before rebinding over the datasource), and compare it to the key field on rowupdating (or perhaps preRender), and cancel the update if they are different, throwing a message box to the user.

[RESOLVED] Stumped on findcontrol in formview of contentpage

I am stumped on drilling down thru the controls, contentplaceholder,masterpage  of a content page to reference a textbox in a formview of contentpage.

I've scoured the net but can't find a likewise example. Any help?

 

This is to reference master page control from ContentPage

this.DisplayPage((Label)this.Master.FindControl("MainContent").FindControl("lblHeader"),
(Label)this.Master.FindControl("MainContent").FindControl("lblMessage"),
(Label)this.Master.FindControl("MainContent").FindControl("lblRedirectHyperLink"));

Here is one more sample, which is probably not what you want, but it's very important - I added it to my public static class

/// <summary>

/// Get the control that caused the PostBack

/// http://ryanfarley.com/blog/blog/archive/2005/03/11/1886.aspx

/// </summary>

/// <returns></returns>

public static Control GetPostBackControl(Page page)

{

Control control = null;

string ctrlname = page.Request.Params.Get("__EVENTTARGET");

if (ctrlname != null && ctrlname != string.Empty)

{

control = page.FindControl(ctrlname);

}

else

{

foreach (string ctl in page.Request.Form)

{

Control c =
null;if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))

{

c = page.FindControl(ctl.Substring(0, ctl.Length - 2));

}

else

{

c = page.FindControl(ctl);

}

if (c is System.Web.UI.WebControls.Button || c is System.Web.UI.WebControls.ImageButton)

{

control = c;

break;

}

}

}

return control;

}

 Can you explain what is happening with your code? Does it take 3 lines to reference a control from the master page? Is what I need to reference (in VB) the contentplaceholder? I am hazy on the hierarchy to begin with but to make clear what I am after is to reference a textbox in my insertItem template of my formview whcih resides in a contentpage (aspx).

 

Hi Fronzie,

In fact this code doesn't help you because it references an object form the master page from the code inside the ContentPage. I believe you're looking for the opposite.

Hi,

You can try implementing a Recursive FindControl method that will drills into child controls in Master Page.

 Perhaps I am, I am getting the null refernce error message. My attempts works when no master page is involved, but now one IS involved. This is the code in question

TBControlName = CType(FormView1.FindControl("Textbox1"), TextBox)

 

Rick Strahl :) Very nice link, I forgot what I saw it there.

I had trouble with that recursive function and I stumbled upon this method. It works but is it sound? Could it be condensed into 1 line of code?

Dim mainContent As ContentPlaceHolder = CType(Master.FindControl("contentplaceholder1"), ContentPlaceHolder)

Dim fv1 As FormView = CType(mainContent.FindControl("formview1"), FormView)

Dim tbcontrol As TextBox = CType(fv1.FindControl("TextBox2"), TextBox)

Response.Write(tbcontrol.Text) ' THIS GIVES VALUE !!!!!

 

 

There is a fairly simple solution here - Referencing data in a Formview

 

Eg: Dim myCategory As String = (DirectCast(formview1.DataItem, DataRowView))("Category")