FindControl in DataList Edit Mode - file-upload

As a new .net/C# web begginner, I always get tripped up when I try to use FindControl. Blam -flat on my face. Here is my current FindControl problem:
I have an .aspx page and Form, then ajax updatePanel, inside it there is my DataList (DataList1) that has an EditItemTemplate: that has the following:
<EditItemTemplate>
<asp:Label ID="thumbnailUploadLabel" runat="server" text="Upload a new thumbnail image:"/><br />
<asp:FileUpload ID="thumbnailImageUpload" runat="server" />
<asp:Button ID="thunbnailImageUploadButton" runat="server" Text="Upload Now" OnClick="thumbnailUpload"/><br />
</EditItemTemplate>
In my C# code behind I have the OnClick code for the fileUpload object:
protected void thumbnailUpload(object s, EventArgs e)
{
if (thumbnailImageUpload.HasFile)
{
//get name of the file & upload
string imageName = thumbnailImageUpload.FileName;
thumbnailImageUpload.SaveAs(MapPath("../../images/merch_sm/" + imageName));
//let'em know that it worked (or didn't)
thumbnailUploadLabel.Text = "Image " + imageName + "has been uploaded.";
}
else
{
thumbnailUploadLabel.Text = "Please choose a thumbnail image to upload.";
}
So of course I'm getting "Object reference not set to an instance of an object" for the FileUpload and the Label.
What is the correct syntax to find these controls, before dealing with them in the OnClick event?
The only way Ive used FindControl is something like:
label thumbnailUploadLabel = DataList1.FindControl("thumbnailUploadLabel") as Label;
But of course this is throwing the "Object reference not set to an instance of an object" error. Any help is very much appreciated.
(I've also seen the 'recursive' code out there that is supposed to make using FindControl easier. Ha! I'm so green at C# that I don't even know how to incorporate those into my project.)
Thanks to all for taking a look at this.

I know this is a hell lot late but i was looking for questions to answer....you must have figured it by now but still
if you add these lines in your code
protected void thumbnailUpload(object sender, EventArgs e)
{
FileUpload thumbnailImageUpload =(FileUpload)DataList1.Items[DataList1.EditItemIndex].FindControl("thumbnailImageUpload");
Label thumbnailUploadLabel = (Label)DataList1.Items[DataList1.EditItemIndex].FindControl("thumbnailUploadLabel");
if (thumbnailImageUpload.HasFile)
{
//Your code here
}
else
{
thumbnailUploadLabel.Text = "Please choose a thumbnail image to upload.";
}
}
this will find the appropriate control for the row you are editing...
also keep your Datalist out of the UPdate Panel beacuse Update Panels are not compatible with FileUpload. if you do the code will run but it will always show thumbnailImageUpload.HasFile as False.

Related

PeopleEditor in usercontrol (Visual webpart) misbehave in if (!IsPostBack)-SharePoint

I have a PeopleEditor in usercontrol (Visual webpart). I try to set the value of PeopleEditor field on page load through below code.
But it is empty, and it works fine other than if (!IsPostBack) part.
if (!IsPostBack)
{
PeopleEditor box1 = (PeopleEditor)GridViewMatrix.Rows[0].Cells[1].FindControl("PeopleEditorApprover");
box1.CommaSeparatedAccounts="domain\user";
}
<spuc:PeopleEditor ID="PeopleEditor1" runat="server" Width="100%" class="fieldName" CommaSeparatedAccounts='<%# Bind("Approver")%>'
AllowEmpty="true" MultiSelect="false" SelectionSet="User,SecGroup,SPGroup" Font-Size="Medium"/>
Any solution?

Changing text values on labels and buttons

I hope this is a simple question, I'm new at Mono, and I'm having trouble getting to grips with GTK#'s bindings.
Essentially, I want to be able to programmatically change attributes against objects like labels, buttons and lists added in by the designer in MonoDevelop.
I know this can be done by instantiating a new instance of say, a button as such:
Button button1 = new Button("Text for button here");
However, say button1 was already created, how would I grab button1 to make changes to it?
Sorry if this all comes across a little thick, I'm still getting the hang of OOP.
Thanks!
This is actually easy enough, now I've sat and worked it out:
protected void OnButton1Clicked (object sender, EventArgs e)
{
Button theButton = (Button)button1;
theButton.Label = "New label text!!";
}

User Control Button Not Firing on Sharepoint Webpart

I have a button that I am adding to a usercontrol(.ascx) that is being displayed in sharepoint as a webpart.
<asp:Button ID="btnCCA_UserAdministration_NewUser" runat="server" Text="Request User" OnClick="btnCCA_UserAdministration_NewUser_Click" >
However the button click action will not fire:
protected void btnCCA_UserAdministration_NewUser_Click(object sender, EventArgs e)
{
btnCCA_UserAdministration_NewUser.Visible = false;
}
I am not getting any error messages or anything. The onload events seem to be working fine. Any thoughts on what I am doing wrong?
we need more .ascx code, and try to change "to any CPU" in solutions

Telerik Rad Button Dyanamically check

Have been unable to dynamically check a Telerik Rad Button control from the code behind in vb.net code. I have used things like chk_TypeEmployee.Checked = True with no result.
<telerik:RadButton ID="chk_TypeEmployee" runat="server" ToggleType="Radio" ButtonType="StandardButton" GroupName="StandardButton" Text="Employee"></telerik:RadButton>
<telerik:RadButton ID="chk_TypeAgency" runat="server" ToggleType="Radio" ButtonType="StandardButton" GroupName="StandardButton" Text="Agency"></telerik:RadButton>
You need to use ToggleStates:
<telerik:RadButton ID="chk_Type" runat="server" ToggleType="Radio" ButtonType="StandardButton" GroupName="StandardButton" Checked="true">
<ToggleStates>
<telerik:RadButtonToggleState Value="Employee" Text="Employee" PrimaryIconCssClass="rbToggleRadioChecked" />
<telerik:RadButtonToggleState Value="Agency" Text="Agency" PrimaryIconCssClass="rbToggleRadio" />
</ToggleStates>
</telerik:RadButton>
To set dynamicaly the button to "Agency" state, assuming it is the second ToggleState for the button:
chk_Type.ToggleStates[0].Selected = false;
chk_Type.ToggleStates[1].Selected = true;
Could be rewritten cleaner this way:
string DBValue = "Agency";
foreach (RadButtonToggleState state in chk_Type.ToggleStates)
{
state.Selected = state.Value.Equals(DBValue);
}
And to retrieve the selected value, use the chk_Type.SelectedToggleState.Value.
Note: The RadButton's property Checked must be set to true. And at least 1 of your toggle states should be Selected, else the first toggle state would be selected by default. In order to use more than 2 toggle states, please read about ToggleType="Custom".
Read more about Telerik's Toggle Buttons.

SharePoint 2010: Create "Alert Me" button

Hopefully this is an easy one...
I need to create an "Alert Me" button on my custom SharePoint masterpage which when clicked, will direct the user to a pre-populated "New Alert" page for that particular site/list. The OOTB blog site template already features this exact same button at the bottom of the default.aspx page, it's markup is as follows:
http://server/currentsite/_layouts/SubNew.aspx?List={00000000-0000-0000-0000-000000000000}&Source=http://server/currentsite/default.aspx
Does anyone know if there is an OOTB control or web part that I can just drop into my page layout to reproduce this?
Obviously I could create the button dynamically in the codebehind of my page layout if need be, but I'd be surprised if there isn't a pre-written control already.
Thanks in advance...
For those insterested, I ended up rolling my own usercontrol for this. Code is as follows:
HTML
<asp:HyperLink ID="AlertHyperLink" runat="server"><img alt="Alert me" src="/_layouts/images/menualert.gif" title="Alert me to any changes that get made to this site." /></asp:HyperLink>
C#
protected void Page_PreRender(object sender, EventArgs e)
{
// If the current page is not associated with a list, then hide the list-sensitive tools.
if (SPContext.Current.List != null)
{
this.AlertHyperLink.NavigateUrl = string.Format(
"{0}/_layouts/SubNew.aspx?List={{{1}}}&Source={2}",
SPContext.Current.Web.Url,
SPContext.Current.List.ID.ToString(),
this.Server.UrlEncode(this.Page.Request.Url.ToString()));
}
else
{
this.AlertHyperLink.Visible = false;
}
}