Invoking the Details View "Item Inserting" event

I  have invoked the Modal Pop up extender, from the "Save" button of the Details View(insert mode) control.
 Now I need to invoke the  DetailsView "Item Inserting" event From the "Send" button on the Modal pop up extender.
Previously,this event was being invoked automatically from the "Save" button.


Urgently required

LVL 1
Ammar IqbalSenior IT Consultant/senior Software engineerAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
abdkhlaifConnect With a Mentor Commented:
in the .aspx page, make sure you have this code for your DetailsView control:
OnItemInserting="DetailsView1_ItemInserting"

and remove this line from btnSendEmail_Click:
DetailsView1.InsertItem(false);

then, move the email functionality to DetailsView1_ItemInserting
0
 
abdkhlaifConnect With a Mentor Commented:
Call InsertItem method which will fire the Item Inserting event for you

protected void Button3_Click(object sender, EventArgs e)
{
	DetailsView1.InsertItem(true);
}

Open in new window

0
 
Ammar IqbalSenior IT Consultant/senior Software engineerAuthor Commented:
Hi,
Thanks for solution. I have invoked the Details View Item Inserting event handler by DEtailsView1.InsertItem(false) --where false indicates the causevalidation.
If we keep it true then we are getting the  un handled Http Exception which is Page.IsValid can not be called before validation has taken place.

Secondly,I have invoked the item inserting event handler from this line as well:
DetailsView1.ItemInserting+=new DetailsViewInsertEventHandler(DetailsView1_ItemInserting);

For this ,I need to set the CommandName  to  "Insert "in the asp.Button  control property (.aspx file)

I have small bit of doubt for one thing. I need to hide the modal pop up after the record has been inserted.
Here is the flow of this event handler: First of all there is an email functionality, then then ItemINserting event is invoked, after that when the record is successfully added to db, I need to hide the Modal pop up dialog.
What should I do to hide it. Since the modal pop is in the details view control, I can not access its "id" in the code behind. So I can not call the hide method directly.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Ammar IqbalSenior IT Consultant/senior Software engineerAuthor Commented:
is anyone there?
0
 
abdkhlaifCommented:
You can access any control on your page from the code using the Page.FindControl method:
using AjaxControlToolkit;
...
ModalPopupExtender mpe = (ModalPopupExtender)Page.FindControl("ModalPopupExtender1");
mpe.Show();
mpe.Hide();

Open in new window

0
 
Ammar IqbalSenior IT Consultant/senior Software engineerAuthor Commented:
Hi,
My Details View item inserting event is being invoked on using (DetailsView1.InsetITem(false). and I can notice that my record is also being inserted, but some how when I add the email functioanity  afterthis line, or in some instances  if I do n't enter email functionality  and only do

Details View1.InsertItem(false). I am getting the following MS J sCript run time error. It is saying that page validation is being already perofmred,
But see the last two lines in the following eerror .I am certain that data which is inserting is valid,and item inserting  event handler is also being invoked properly. In the last 2 lines, it is saying i neeed to register the post bacl or callback validation.

Please have a look at it on an urgent basis

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.

 If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
0
 
abdkhlaifCommented:
attach the code for DetailsView1_ItemInserting
I need to see why you have to invoke Item Inserting event
0
 
Ammar IqbalSenior IT Consultant/senior Software engineerAuthor Commented:
This is what I am doing .
<asp:LinkButton ID="i_insert" runat="server" CausesValidation="False" CommandName="Insert" CssClass="button" Visible="<%# IsAdmin %>">
<span class="ui-icon ui-icon-check"></span>Save
</asp:LinkButton>
-------------------------------------------------------------------


<act:ModalPopupExtender ID="mpUserNotification"  BehaviorID="mpUserNotificationBehavior" runat="server" TargetControlID="i_insert" BackgroundCssClass="modalPopup" 
                                             PopupControlID="divPopup" DropShadow="true" >
</act:ModalPopupExtender>

Open in new window

protected override void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            try
            {
                if (e.Values.Count == 0)
                {
                    fillDataSource();
                    DataView dv = DataSource;
                    DataTable dt = dv.Table;
                    int count = dv.Count;

                    DataRow dr = dt.NewRow();
                    foreach (DataColumn dc in dt.Columns)
                    {
                        object value = getValue(DetailsView1, dc);//, DetailsViewMode.Insert
                        if (value != DBNull.Value || dc.AllowDBNull)
                        {
                            if (dc.ColumnName == "id")
                            {
                                count = count + 1;
                                value = count;
                            }
                            if (dc.ColumnName == "datetime")
                            {
                                DateTime current = DateTime.Now;
                                value = current;
                            }
                           /*
                            if (dc.ColumnName == "image")
                            {
                                string file=value.ToString();
                                value = System.IO.Path.GetFullPath(file);
                            }
                            */
                            if (dc.ColumnName == "instrument")
                            {
                                if (value == "")
                                {
                                    value = DBNull.Value;
                                }
                                else
                                {
                                    
                                    DataSet dsSensorId = new DataSet();
                                    dsSensorId = getSensorName(value.ToString());
                                    object id = dsSensorId.Tables["Instrument"].Rows[0].ItemArray[0].ToString();
                                    value = id;
                                }
                            }
                          
                            dr[dc] = value;
                        }
                     }
                    dt.Rows.Add(dr);
                    int ct = dt.ParentRelations.Count;
                    addMessage(dataprovider.updateTable(dr.Table),0);
                    //updateTable(dr.Table);
                }

            }
            catch (SqlException sex)
            {
                if (sex.Number == 2601) addMessage(addMessage("This name is already taken!", 1));//se.Number==2160
                else addMessage(addMessage(sex));
            }//end try2_catch2
            catch (Exception ex)
            {
                addMessage(addMessage(ex));
            }//end try2_catch2

        }

Open in new window

protected void btnSendEmail_Click(object sender,EventArgs e)
        {
            //DetailsView1.ItemInserting+=new DetailsViewInsertEventHandler(DetailsView1_ItemInserting);
            DetailsView1.InsertItem(false);
            
            string[] toaddress = null;
            string strtoaddresses = string.Empty;
            ListBox ToList = (ListBox)DetailsView1.FindControl("listTo");
            if (ToList != null)
            {
                if (ToList.Items != null)
                {
                    int listcount = ToList.Items.Count;
                    toaddress = new string[listcount];
                    for (int i = 0; i < listcount; i++)
                    {
                        toaddress[i] = ToList.Items[i].ToString();
                    }

                    //making a semi colon separated string
                    for (int i = 0; i < listcount; i++)
                    {
                        if (i < listcount - 2) strtoaddresses += toaddress[i] + ',';
                        if (i == listcount - 1) strtoaddresses += toaddress[i];
                    }

                    MailMessage mailMsg = new MailMessage();
                    MailMessageEventArgs me = new MailMessageEventArgs(mailMsg);
                    SmtpClient smtpClient = new SmtpClient();
                    mailMsg.From = me.Message.From;
                    mailMsg.To.Add(strtoaddresses);
                    mailMsg.Subject = "Test Email Log Module";
                    mailMsg.IsBodyHtml = true;
                    mailMsg.Body = " Introducing the Cautus Geo's  Munin Log Module";
                    try
                    {
                        smtpClient.EnableSsl = true;
                        smtpClient.Send(mailMsg);

                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.Message);

                    }
                   
                }


            } 
         
       ScriptManager.RegisterStartupScript(Page, this.GetType(), "ClosePopup", "<script>$find('mpUserNotificationBehavior').hide();</script>", false);

        
        }

Open in new window

0
 
Ammar IqbalSenior IT Consultant/senior Software engineerAuthor Commented:
If i am not using the modal pop up extender, then DEtails View item inserting event handler is being automatically invked from the savew button. Now its' being invoked from the button on the post back.I ned to insert recrod, ,send email to different users, and hide the dialog...

I am able to insert the record, but some howthis run time jscript error is coming when I am introducing the email functioanlity .
0
 
Ammar IqbalSenior IT Consultant/senior Software engineerAuthor Commented:
are uthere?
0
 
Ammar IqbalSenior IT Consultant/senior Software engineerAuthor Commented:
Areu sure.I donæt have this  on my .aspx page.
Because when I was invoking it on the Save button in a normal way visa "Save" button. I do not require this.

In this case when i want to invoke item inserting  via the button on a modal pop up? Do you think,i need to do this..?
0
 
Ammar IqbalSenior IT Consultant/senior Software engineerAuthor Commented:
same problem.... I have removed  DEtailsView1.insertItem form btnSend, Record is not inserted andsame error is coming
0
 
Ammar IqbalSenior IT Consultant/senior Software engineerAuthor Commented:
Somehow I got hold of hat was wrong in it.. Somehow, in my db table total numbr of records were 21,  and in the primary key column the id of the first record was 22, ,This id was subsequently increased by 1 as it traverses through the rest of the 21 records....
When ever i  was trying to insert a new record,  the id of the record should be assigned 22, but that is alredy assigned, and it was raising the Exception. that is why it was not inserting,and i was pressing the send button again and agian , thus the java script run time erorr of postbacks and other stuff....
now its solved, but it is inserting 3 records at a time, i need to look into m
0
 
Ammar IqbalSenior IT Consultant/senior Software engineerAuthor Commented:
i need to look into my logic, whats wrong in it.  Thanks for looking into it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.