I am using webforms model binding on EF6 code first project. I have a page that was working perfectly. It stopped working with no apparent reason. The insert method is not called anymore. I have another insert page and it works perfectly still. Anyone know of anything that would cause this?
Here is some of the markup that it is using.
<asp:FormView runat="server" ID="fvTour"
ItemType="CommonInterface.Tour" DefaultMode="Insert"
InsertItemPosition="FirstItem" InsertMethod="InsertItem"
OnItemCommand="ItemCommand" RenderOuterTable="false">
Code behind
public void InsertItem()
{
using (_db)
{
FileUpload picUpload = (FileUpload)fvTour.FindControl("fuploadTour");
if (picUpload == null || !picUpload.HasFile) return;
var item = new CommonInterface.Tour();
item.MemberID = (int)Session["MemberID"];
item.FID = LoginBLL.GetFID(item.MemberID);
item.TourSubmitDate = DateTime.Now;
item.PID = null;
item.TourState = ((DropDownList)fvTour.FindControl("ddlState")).SelectedItem.Text;
item.TourCounty = ((DropDownList)fvTour.FindControl("ddlCounties")).SelectedItem.Text;
item.TourLayout = ((DropDownList)fvTour.FindControl("ddlLayout")).SelectedItem.Text;
item.TourType = ((DropDownList)fvTour.FindControl("ddlType")).SelectedItem.Text;
TryUpdateModel(item);
if (ModelState.IsValid)
{
// Save changes
_db.Tours.Add(item);
_db.SaveChanges();
Response.Redirect("Default");
}
}
}
Edit 2:
Try to use OnItemCommand and see if that works. Set OnItemCommand="ItemCommand" back to your markup and try the following on code behind:
public void ItemCommand(object sender, FormViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
InsertItem();
}
}
Make sure that the button you press has a CommandName="Insert" attribute set.
You should be able to set a breakpoint on this method at least. Then you can see if the button fires an event at all.
Related
I am using the following code to bind image from the database to my asp:Image control in the RadGrid Mastertable. However I am getting a null reference exceptionon the line " iP.ImageUrl = "~/StreamImage.ashx?Id=" + user;" and I am not able to understand why is it giving me that
Here is the code:
<asp:Image ID="iPhoto1" runat="server" AlternateText="Profile Picture"
/>
protected void btnSearch_Click(object sender, EventArgs e)
{
var data = ss.qStudSearch(Common.GetValue(txtName.Text),
Common.GetValue(txtResearch.Text), currUser.iProgramID);
rgSearch.DataSource = data;
rgSearch.DataBind();
foreach (GridDataItem d in rgSearch.MasterTableView.Items)
{
int user = Convert.ToInt32(d.GetDataKeyValue("upid"));
var pix = ss.GetPics(user);
Image iP =
(Image)rgSearch.MasterTableView.FindControl("iPhoto1");
if (pix != null)
{
iP.ImageUrl = "~/StreamImage.ashx?Id=" + user;
}
else
{
iP.ImageUrl = "~/images/no_image.jpg";
}
}
}
The getpics method retrieves the images from the database and it is retrieving it properly as I checked it by debugging. However I am not able to assign it to the asp:Image control.. Can someone help me with this?
It is likely that either iP is null, or user is null. Put a breakpoint before the assignment of iP.ImageUrl and inspect the items to see which one is null.
You can bind directly using binary image column but it's got slow performance with the data:
<telerik:GridBinaryImageColumn DataField="image" HeaderText="Photo" AllowFiltering="false"
UniqueName="LogoView" ImageAlign="NotSet" ImageHeight="80px" ImageWidth="80px"
ResizeMode="Fit" DataAlternateTextField="image">
<HeaderStyle Width="10%" />
</telerik:GridBinaryImageColumn>
I had everything working fine then when I put in the Captcha control it stopped going to the function where it creates the user.
So if I remove the control from the aspx page it works fine and goes directly to the CreatingUser function.
-- aspx --
<asp:CreateUserWizard ID="RegisterUser" runat="server" EnableViewState="true"
OnCreatedUser="RegisterUser_CreatedUser"
OnCreatingUser="RegisterUser_CreatingUser">
<cc1:CaptchaControl id="CControl" runat="server" CaptchaBackgroundNoise="Low" CaptchaFontWarping="Low" CaptchaLineNoise="Low" CaptchaMaxTimeout="300" ></cc1:CaptchaControl>
--Behind Code--
protected void RegisterUser_CreatingUser(object sender, LoginCancelEventArgs e)
{
Captcha.V2.CaptchaControl mycaptcha = (Captcha.V2.CaptchaControl)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("CControl");
mycaptcha.Validate();
Boolean answer = mycaptcha.IsValid;
if (answer == false)
{
e.Cancel = true;
}
// Testing area where it worked before just to see...
//
// .....
}
I have a textbox with a live search function. It is working all good except one problem. If I type any characters on it, it just loses its focus. If I set textbox.Focus(), the cursor goes at the beginning of the textbox.
I have tried most of solutions on the internet. Please check my codes below.
asp:TextBox ID="searchCompany" runat="server" Text="" CssClass="searchCompany" AutoPostBack="true" Width="190px" OnTextChanged="searchCompany_TextChanged"></asp:TextBox>
In page_Load
protected void Page_Load(object sender, EventArgs e)
{
//ScriptManager1.RegisterAsyncPostBackControl(Menu1);
menuDisplay();
searchCompany.Attributes.Add("onkeyup", "setTimeout('__doPostBack(\\'" + searchCompany.UniqueID + "\\',\\'\\')', 0);");
//searchCompany.Attributes.Add("onfocus", "javascript:setSelectionRange('" + "','')");
//searchCompany.Focus();
}
and I have tried javascript as below
<script type="text/javascript">
function setSelectionRange() {
var inputField = document.getElementById('searchCompany');
if (inputField != null && inputField.value.length > 0) {
if (inputField.createTextRange) {
var FieldRange = inputField.createTextRange();
FieldRange.moveStart('character',inputField.value.length);
FieldRange.collapse();
FieldRange.select();
}
}
}
</script>
I have tried to put codes on a method "searchCompany_TextChanged" which is calling if user type any characters on a textbox everytime however it is not working as well.
I saw other solutions with using Textbox.Select() but System.Windows.Control is not working in asp.net i guess.
Any idea??
There's a very simple trick that's worked for me. Basically, set the text value of the of input to itself to its own text value, and that will move the cursor to the end of the text. Then just focus it. This code uses jQuery to demonstrate that, but you should be able to do that in straight JS as well.
HTML
<input type="text" id="focusText"></input>
<button id="focusButton">Set Focus</button>
JavaScript
$("#focusButton").click(function() {
var text = $("#focusText").val();
$("#focusText").val(text).focus();
})
Here's a non jQuery example of the JavaScript, HTML should be the same:
document.getElementById("focusButton").onclick = function() {
var inputElement = document.getElementById("focusText");
var text = inputElement.value;
inputElement.value = text;
inputElement.focus();
}
Here's a fiddle demonstrating the non-jQuery version of the code: http://jsfiddle.net/C3gCa/
I have a ModalPopupExtender(mpe) which will show when a asp:button is clicked. The button calls a javascript function which then calls a webmethod. The webmethod is in the code behind of the page.
The webmethod passes a string variable to another method in the code behind. which determines what the mpe does. The idea behind this logic is that when you click the button I want to enable certain parts of the mpe and disable other parts. This is because the mpe has several functionalities.
Aspx Button and UserControl
<uc2:IMG ID="IMG1" runat="server" />
<asp:Button ID="btnAddNewImg" runat="server" Text="Add New Image" onclientclick="ShowImgPopupScreen()" />
Javascript function
<script type="text/javascript">
function ShowImgPopupScreen() {
// call server side method
PageMethods.AddNewImg();
}
</script>
ASPX.CS code
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static void AddNewImg()
{
string option = "add";
image_loader_cms cms = new image_loader_cms();
cms.SetButtons(option);
}
protected void SetButtons(string add)
{
if (add == "add")
{
IMG1.Add = true;
}
else
{
IMG1.Edit = true;
}
}
ASCX.CS
public bool Add
{
get
{
return btnAdd.Enabled;
}
set
{
btnAdd.Enabled = value;
}
}
public bool Edit
{
get
{
return btnUpdate.Enabled;
}
set
{
btnUpdate.Enabled = value;
}
}
The code steps into IMG1.Add = true; when using breakpoints but after that line of code I get this error message.
Microsoft JScript runtime error: Sys.Net.WebServiceFailedException: The server method 'AddNewImg' failed with the following error:
System.NullReferenceException-- Object reference not set to an instance of an object.
This problem has me scratching my head.
You are creating a new usercontrol, however it wont create its child controls or be in anyway usable until it is added to the control tree, hence your image and buttons are never created.
Since you are in static page method, i dont think this is going to work.
What are you actually trying to do with the user control from the page moethod ?
I have the following working so far in C# on my Gridview called GridView1. It works when I put it in the onSelectedIndexChanged.
HostTextbox.Text = GridView1.SelectedRow.Cells[0].Text;
but since this posts back to the server I want to avoid it because I will be doing it for cells[0] to cells[10]. So, I looked into Javascript. I googled around and found various solutions and this is the one I have "semi-working" so far.
My C# looks like this:
int myRowIdx = 0; // class variable
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("ondbclick", "sample('" + myRowIdx.ToString() + "')");
}
myRowIdx++;
}
In my Javascript I inserted alerts to tell me where the problem happpens. It looks like this:
function sample(rowIn) {
alert("A");
var gViewID = '<%= GridView1.ClientID %>';
alert("B");
var gView = getElementById(gViewID);
alert("C");
var gViewRow = gView.rows[rowIn];
alert("D");
var gViewRowColumn = gViewRow.cells[0];
alert("E");
var displayCell = gViewRowColumn.innerText;
alert("F");
alert(displayCell);
}
B is the last alert I see. I can't seem to figure this out. I looked carefully into it and still no success. Please help.
I don't understand what you mean with "I want to avoid it because I will be doing it for cells[0] to cells[10]". You could do that for each cell in the selected row successively. So you only need one Postback.
According to your Javascript problems, you could simply pass the tr(GridViewRow) as js-variable to your sample-function. Therefore you only have to pass this as parameter:
e.Row.Attributes.Add("ondbclick", "sample(this)");
and in your js-function:
function sample(tr) {
var gViewRowColumn = tr.cells[0];
var displayCell = gViewRowColumn.innerText;
alert(displayCell);
}
Use Item Template Panel for grid view.
ex.http://msdn.microsoft.com/en-us/library/aa479353.aspx
Use the onrowcommand event for get the values.
like..
Event
{
if(e.commandname="Edit")
{
}